Make `Copy all to Clipboard' copy all non-empty lines from the
[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 const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
60
61 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
62 const wchar_t sel_nl[] = SEL_NL;
63
64 /*
65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
69 */
70 #define UCSGET(a, x) \
71 ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \
72 (a)[(x)-1] : (a)[(x)] )
73
74 /*
75 * Internal prototypes.
76 */
77 static unsigned long *resizeline(unsigned long *, int);
78 static unsigned long *lineptr(Terminal *, int, int);
79 static void do_paint(Terminal *, Context, int);
80 static void erase_lots(Terminal *, int, int, int);
81 static void swap_screen(Terminal *, int, int, int);
82 static void update_sbar(Terminal *);
83 static void deselect(Terminal *);
84 static void term_print_finish(Terminal *);
85 #ifdef OPTIMISE_SCROLL
86 static void scroll_display(Terminal *, int, int, int);
87 #endif /* OPTIMISE_SCROLL */
88
89 /*
90 * Resize a line to make it `cols' columns wide.
91 */
92 static unsigned long *resizeline(unsigned long *line, int cols)
93 {
94 int i, oldlen;
95 unsigned long lineattrs;
96
97 if (line[0] != (unsigned long)cols) {
98 /*
99 * This line is the wrong length, which probably means it
100 * hasn't been accessed since a resize. Resize it now.
101 */
102 oldlen = line[0];
103 lineattrs = line[oldlen + 1];
104 line = sresize(line, 2 + cols, TTYPE);
105 line[0] = cols;
106 for (i = oldlen; i < cols; i++)
107 line[i + 1] = ERASE_CHAR;
108 line[cols + 1] = lineattrs & LATTR_MODE;
109 }
110
111 return line;
112 }
113
114 /*
115 * Get the number of lines in the scrollback.
116 */
117 static int sblines(Terminal *term)
118 {
119 int sblines = count234(term->scrollback);
120 if (term->cfg.erase_to_scrollback &&
121 term->alt_which && term->alt_screen) {
122 sblines += term->alt_sblines;
123 }
124 return sblines;
125 }
126
127 /*
128 * Retrieve a line of the screen or of the scrollback, according to
129 * whether the y coordinate is non-negative or negative
130 * (respectively).
131 */
132 static unsigned long *lineptr(Terminal *term, int y, int lineno)
133 {
134 unsigned long *line, *newline;
135 tree234 *whichtree;
136 int treeindex;
137
138 if (y >= 0) {
139 whichtree = term->screen;
140 treeindex = y;
141 } else {
142 int altlines = 0;
143 if (term->cfg.erase_to_scrollback &&
144 term->alt_which && term->alt_screen) {
145 altlines = term->alt_sblines;
146 }
147 if (y < -altlines) {
148 whichtree = term->scrollback;
149 treeindex = y + altlines + count234(term->scrollback);
150 } else {
151 whichtree = term->alt_screen;
152 treeindex = y + term->alt_sblines;
153 /* treeindex = y + count234(term->alt_screen); */
154 }
155 }
156 line = index234(whichtree, treeindex);
157
158 /* We assume that we don't screw up and retrieve something out of range. */
159 assert(line != NULL);
160
161 newline = resizeline(line, term->cols);
162 if (newline != line) {
163 delpos234(whichtree, treeindex);
164 addpos234(whichtree, newline, treeindex);
165 line = newline;
166 }
167
168 return line + 1;
169 }
170
171 #define lineptr(x) lineptr(term,x,__LINE__)
172
173 /*
174 * Set up power-on settings for the terminal.
175 */
176 static void power_on(Terminal *term)
177 {
178 term->curs.x = term->curs.y = 0;
179 term->alt_x = term->alt_y = 0;
180 term->savecurs.x = term->savecurs.y = 0;
181 term->alt_t = term->marg_t = 0;
182 if (term->rows != -1)
183 term->alt_b = term->marg_b = term->rows - 1;
184 else
185 term->alt_b = term->marg_b = 0;
186 if (term->cols != -1) {
187 int i;
188 for (i = 0; i < term->cols; i++)
189 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
190 }
191 term->alt_om = term->dec_om = term->cfg.dec_om;
192 term->alt_ins = term->insert = FALSE;
193 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
194 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
195 term->alt_cset = term->cset = term->save_cset = 0;
196 term->alt_utf = term->utf = term->save_utf = 0;
197 term->utf_state = 0;
198 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
199 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
200 term->rvideo = 0;
201 term->in_vbell = FALSE;
202 term->cursor_on = 1;
203 term->big_cursor = 0;
204 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
205 term->term_editing = term->term_echoing = FALSE;
206 term->app_cursor_keys = term->cfg.app_cursor;
207 term->app_keypad_keys = term->cfg.app_keypad;
208 term->use_bce = term->cfg.bce;
209 term->blink_is_real = term->cfg.blinktext;
210 term->erase_char = ERASE_CHAR;
211 term->alt_which = 0;
212 term_print_finish(term);
213 {
214 int i;
215 for (i = 0; i < 256; i++)
216 term->wordness[i] = term->cfg.wordness[i];
217 }
218 if (term->screen) {
219 swap_screen(term, 1, FALSE, FALSE);
220 erase_lots(term, FALSE, TRUE, TRUE);
221 swap_screen(term, 0, FALSE, FALSE);
222 erase_lots(term, FALSE, TRUE, TRUE);
223 }
224 }
225
226 /*
227 * Force a screen update.
228 */
229 void term_update(Terminal *term)
230 {
231 Context ctx;
232 ctx = get_ctx(term->frontend);
233 if (ctx) {
234 int need_sbar_update = term->seen_disp_event;
235 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
236 term->disptop = 0; /* return to main screen */
237 term->seen_disp_event = 0;
238 need_sbar_update = TRUE;
239 }
240 if (need_sbar_update)
241 update_sbar(term);
242 do_paint(term, ctx, TRUE);
243 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
244 free_ctx(ctx);
245 }
246 }
247
248 /*
249 * Called from front end when a keypress occurs, to trigger
250 * anything magical that needs to happen in that situation.
251 */
252 void term_seen_key_event(Terminal *term)
253 {
254 /*
255 * On any keypress, clear the bell overload mechanism
256 * completely, on the grounds that large numbers of
257 * beeps coming from deliberate key action are likely
258 * to be intended (e.g. beeps from filename completion
259 * blocking repeatedly).
260 */
261 term->beep_overloaded = FALSE;
262 while (term->beephead) {
263 struct beeptime *tmp = term->beephead;
264 term->beephead = tmp->next;
265 sfree(tmp);
266 }
267 term->beeptail = NULL;
268 term->nbeeps = 0;
269
270 /*
271 * Reset the scrollback on keypress, if we're doing that.
272 */
273 if (term->cfg.scroll_on_key) {
274 term->disptop = 0; /* return to main screen */
275 term->seen_disp_event = 1;
276 }
277 }
278
279 /*
280 * Same as power_on(), but an external function.
281 */
282 void term_pwron(Terminal *term)
283 {
284 power_on(term);
285 if (term->ldisc) /* cause ldisc to notice changes */
286 ldisc_send(term->ldisc, NULL, 0, 0);
287 fix_cpos;
288 term->disptop = 0;
289 deselect(term);
290 term_update(term);
291 }
292
293 /*
294 * When the user reconfigures us, we need to check the forbidden-
295 * alternate-screen config option, disable raw mouse mode if the
296 * user has disabled mouse reporting, and abandon a print job if
297 * the user has disabled printing.
298 */
299 void term_reconfig(Terminal *term, Config *cfg)
300 {
301 /*
302 * Before adopting the new config, check all those terminal
303 * settings which control power-on defaults; and if they've
304 * changed, we will modify the current state as well as the
305 * default one. The full list is: Auto wrap mode, DEC Origin
306 * Mode, BCE, blinking text, character classes.
307 */
308 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
309 int i;
310
311 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
312 reset_decom = (term->cfg.dec_om != cfg->dec_om);
313 reset_bce = (term->cfg.bce != cfg->bce);
314 reset_blink = (term->cfg.blinktext != cfg->blinktext);
315 reset_charclass = 0;
316 for (i = 0; i < lenof(term->cfg.wordness); i++)
317 if (term->cfg.wordness[i] != cfg->wordness[i])
318 reset_charclass = 1;
319
320 term->cfg = *cfg; /* STRUCTURE COPY */
321
322 if (reset_wrap)
323 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
324 if (reset_decom)
325 term->alt_om = term->dec_om = term->cfg.dec_om;
326 if (reset_bce) {
327 term->use_bce = term->cfg.bce;
328 if (term->use_bce)
329 term->erase_char = (' ' | ATTR_ASCII |
330 (term->curr_attr &
331 (ATTR_FGMASK | ATTR_BGMASK)));
332 else
333 term->erase_char = ERASE_CHAR;
334 }
335 if (reset_blink)
336 term->blink_is_real = term->cfg.blinktext;
337 if (reset_charclass)
338 for (i = 0; i < 256; i++)
339 term->wordness[i] = term->cfg.wordness[i];
340
341 if (term->cfg.no_alt_screen)
342 swap_screen(term, 0, FALSE, FALSE);
343 if (term->cfg.no_mouse_rep) {
344 term->xterm_mouse = 0;
345 set_raw_mouse_mode(term->frontend, 0);
346 }
347 if (term->cfg.no_remote_charset) {
348 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
349 term->sco_acs = term->alt_sco_acs = 0;
350 term->utf = 0;
351 }
352 if (!*term->cfg.printer) {
353 term_print_finish(term);
354 }
355 }
356
357 /*
358 * Clear the scrollback.
359 */
360 void term_clrsb(Terminal *term)
361 {
362 unsigned long *line;
363 term->disptop = 0;
364 while ((line = delpos234(term->scrollback, 0)) != NULL) {
365 sfree(line);
366 }
367 term->tempsblines = 0;
368 term->alt_sblines = 0;
369 update_sbar(term);
370 }
371
372 /*
373 * Initialise the terminal.
374 */
375 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
376 void *frontend)
377 {
378 Terminal *term;
379
380 /*
381 * Allocate a new Terminal structure and initialise the fields
382 * that need it.
383 */
384 term = snew(Terminal);
385 term->frontend = frontend;
386 term->ucsdata = ucsdata;
387 term->cfg = *mycfg; /* STRUCTURE COPY */
388 term->logctx = NULL;
389 term->compatibility_level = TM_PUTTY;
390 strcpy(term->id_string, "\033[?6c");
391 term->last_blink = term->last_tblink = 0;
392 term->paste_buffer = NULL;
393 term->paste_len = 0;
394 term->last_paste = 0;
395 bufchain_init(&term->inbuf);
396 bufchain_init(&term->printer_buf);
397 term->printing = term->only_printing = FALSE;
398 term->print_job = NULL;
399 term->vt52_mode = FALSE;
400 term->cr_lf_return = FALSE;
401 term->seen_disp_event = FALSE;
402 term->xterm_mouse = term->mouse_is_down = FALSE;
403 term->reset_132 = FALSE;
404 term->blinker = term->tblinker = 0;
405 term->has_focus = 1;
406 term->repeat_off = FALSE;
407 term->termstate = TOPLEVEL;
408 term->selstate = NO_SELECTION;
409 term->curstype = 0;
410
411 term->screen = term->alt_screen = term->scrollback = NULL;
412 term->tempsblines = 0;
413 term->alt_sblines = 0;
414 term->disptop = 0;
415 term->disptext = term->dispcurs = NULL;
416 term->tabs = NULL;
417 deselect(term);
418 term->rows = term->cols = -1;
419 power_on(term);
420 term->beephead = term->beeptail = NULL;
421 #ifdef OPTIMISE_SCROLL
422 term->scrollhead = term->scrolltail = NULL;
423 #endif /* OPTIMISE_SCROLL */
424 term->nbeeps = 0;
425 term->lastbeep = FALSE;
426 term->beep_overloaded = FALSE;
427 term->attr_mask = 0xffffffff;
428 term->resize_fn = NULL;
429 term->resize_ctx = NULL;
430 term->in_term_out = FALSE;
431
432 return term;
433 }
434
435 void term_free(Terminal *term)
436 {
437 unsigned long *line;
438 struct beeptime *beep;
439
440 while ((line = delpos234(term->scrollback, 0)) != NULL)
441 sfree(line);
442 freetree234(term->scrollback);
443 while ((line = delpos234(term->screen, 0)) != NULL)
444 sfree(line);
445 freetree234(term->screen);
446 while ((line = delpos234(term->alt_screen, 0)) != NULL)
447 sfree(line);
448 freetree234(term->alt_screen);
449 sfree(term->disptext);
450 while (term->beephead) {
451 beep = term->beephead;
452 term->beephead = beep->next;
453 sfree(beep);
454 }
455 bufchain_clear(&term->inbuf);
456 if(term->print_job)
457 printer_finish_job(term->print_job);
458 bufchain_clear(&term->printer_buf);
459 sfree(term->paste_buffer);
460 sfree(term);
461 }
462
463 /*
464 * Set up the terminal for a given size.
465 */
466 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
467 {
468 tree234 *newalt;
469 unsigned long *newdisp, *line;
470 int i, j;
471 int sblen;
472 int save_alt_which = term->alt_which;
473
474 if (newrows == term->rows && newcols == term->cols &&
475 newsavelines == term->savelines)
476 return; /* nothing to do */
477
478 deselect(term);
479 swap_screen(term, 0, FALSE, FALSE);
480
481 term->alt_t = term->marg_t = 0;
482 term->alt_b = term->marg_b = newrows - 1;
483
484 if (term->rows == -1) {
485 term->scrollback = newtree234(NULL);
486 term->screen = newtree234(NULL);
487 term->tempsblines = 0;
488 term->rows = 0;
489 }
490
491 /*
492 * Resize the screen and scrollback. We only need to shift
493 * lines around within our data structures, because lineptr()
494 * will take care of resizing each individual line if
495 * necessary. So:
496 *
497 * - If the new screen is longer, we shunt lines in from temporary
498 * scrollback if possible, otherwise we add new blank lines at
499 * the bottom.
500 *
501 * - If the new screen is shorter, we remove any blank lines at
502 * the bottom if possible, otherwise shunt lines above the cursor
503 * to scrollback if possible, otherwise delete lines below the
504 * cursor.
505 *
506 * - Then, if the new scrollback length is less than the
507 * amount of scrollback we actually have, we must throw some
508 * away.
509 */
510 sblen = count234(term->scrollback);
511 /* Do this loop to expand the screen if newrows > rows */
512 assert(term->rows == count234(term->screen));
513 while (term->rows < newrows) {
514 if (term->tempsblines > 0) {
515 /* Insert a line from the scrollback at the top of the screen. */
516 assert(sblen >= term->tempsblines);
517 line = delpos234(term->scrollback, --sblen);
518 term->tempsblines -= 1;
519 addpos234(term->screen, line, 0);
520 term->curs.y += 1;
521 term->savecurs.y += 1;
522 } else {
523 /* Add a new blank line at the bottom of the screen. */
524 line = snewn(newcols + 2, TTYPE);
525 line[0] = newcols;
526 for (j = 0; j < newcols; j++)
527 line[j + 1] = ERASE_CHAR;
528 line[newcols + 1] = LATTR_NORM;
529 addpos234(term->screen, line, count234(term->screen));
530 }
531 term->rows += 1;
532 }
533 /* Do this loop to shrink the screen if newrows < rows */
534 while (term->rows > newrows) {
535 if (term->curs.y < term->rows - 1) {
536 /* delete bottom row, unless it contains the cursor */
537 sfree(delpos234(term->screen, term->rows - 1));
538 } else {
539 /* push top row to scrollback */
540 line = delpos234(term->screen, 0);
541 addpos234(term->scrollback, line, sblen++);
542 term->tempsblines += 1;
543 term->curs.y -= 1;
544 term->savecurs.y -= 1;
545 }
546 term->rows -= 1;
547 }
548 assert(term->rows == newrows);
549 assert(count234(term->screen) == newrows);
550
551 /* Delete any excess lines from the scrollback. */
552 while (sblen > newsavelines) {
553 line = delpos234(term->scrollback, 0);
554 sfree(line);
555 sblen--;
556 }
557 if (sblen < term->tempsblines)
558 term->tempsblines = sblen;
559 assert(count234(term->scrollback) <= newsavelines);
560 assert(count234(term->scrollback) >= term->tempsblines);
561 term->disptop = 0;
562
563 /* Make a new displayed text buffer. */
564 newdisp = snewn(newrows * (newcols + 1), TTYPE);
565 for (i = 0; i < newrows * (newcols + 1); i++)
566 newdisp[i] = ATTR_INVALID;
567 sfree(term->disptext);
568 term->disptext = newdisp;
569 term->dispcurs = NULL;
570
571 /* Make a new alternate screen. */
572 newalt = newtree234(NULL);
573 for (i = 0; i < newrows; i++) {
574 line = snewn(newcols + 2, TTYPE);
575 line[0] = newcols;
576 for (j = 0; j < newcols; j++)
577 line[j + 1] = term->erase_char;
578 line[newcols + 1] = LATTR_NORM;
579 addpos234(newalt, line, i);
580 }
581 if (term->alt_screen) {
582 while (NULL != (line = delpos234(term->alt_screen, 0)))
583 sfree(line);
584 freetree234(term->alt_screen);
585 }
586 term->alt_screen = newalt;
587 term->alt_sblines = 0;
588
589 term->tabs = sresize(term->tabs, newcols, unsigned char);
590 {
591 int i;
592 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
593 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
594 }
595
596 /* Check that the cursor positions are still valid. */
597 if (term->savecurs.y < 0)
598 term->savecurs.y = 0;
599 if (term->savecurs.y >= newrows)
600 term->savecurs.y = newrows - 1;
601 if (term->curs.y < 0)
602 term->curs.y = 0;
603 if (term->curs.y >= newrows)
604 term->curs.y = newrows - 1;
605 if (term->curs.x >= newcols)
606 term->curs.x = newcols - 1;
607 term->alt_x = term->alt_y = 0;
608 term->wrapnext = term->alt_wnext = FALSE;
609
610 term->rows = newrows;
611 term->cols = newcols;
612 term->savelines = newsavelines;
613 fix_cpos;
614
615 swap_screen(term, save_alt_which, FALSE, FALSE);
616
617 update_sbar(term);
618 term_update(term);
619 if (term->resize_fn)
620 term->resize_fn(term->resize_ctx, term->cols, term->rows);
621 }
622
623 /*
624 * Hand a function and context pointer to the terminal which it can
625 * use to notify a back end of resizes.
626 */
627 void term_provide_resize_fn(Terminal *term,
628 void (*resize_fn)(void *, int, int),
629 void *resize_ctx)
630 {
631 term->resize_fn = resize_fn;
632 term->resize_ctx = resize_ctx;
633 if (term->cols > 0 && term->rows > 0)
634 resize_fn(resize_ctx, term->cols, term->rows);
635 }
636
637 /* Find the bottom line on the screen that has any content.
638 * If only the top line has content, returns 0.
639 * If no lines have content, return -1.
640 */
641 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
642 {
643 int i;
644 for (i = count234(screen) - 1; i >= 0; i--) {
645 unsigned long *line = index234(screen, i);
646 int j;
647 int cols = line[0];
648 for (j = 0; j < cols; j++) {
649 if (line[j + 1] != term->erase_char) break;
650 }
651 if (j != cols) break;
652 }
653 return i;
654 }
655
656 /*
657 * Swap screens. If `reset' is TRUE and we have been asked to
658 * switch to the alternate screen, we must bring most of its
659 * configuration from the main screen and erase the contents of the
660 * alternate screen completely. (This is even true if we're already
661 * on it! Blame xterm.)
662 */
663 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
664 {
665 int t;
666 tree234 *ttr;
667
668 if (!which)
669 reset = FALSE; /* do no weird resetting if which==0 */
670
671 if (which != term->alt_which) {
672 term->alt_which = which;
673
674 ttr = term->alt_screen;
675 term->alt_screen = term->screen;
676 term->screen = ttr;
677 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
678 t = term->curs.x;
679 if (!reset && !keep_cur_pos)
680 term->curs.x = term->alt_x;
681 term->alt_x = t;
682 t = term->curs.y;
683 if (!reset && !keep_cur_pos)
684 term->curs.y = term->alt_y;
685 term->alt_y = t;
686 t = term->marg_t;
687 if (!reset) term->marg_t = term->alt_t;
688 term->alt_t = t;
689 t = term->marg_b;
690 if (!reset) term->marg_b = term->alt_b;
691 term->alt_b = t;
692 t = term->dec_om;
693 if (!reset) term->dec_om = term->alt_om;
694 term->alt_om = t;
695 t = term->wrap;
696 if (!reset) term->wrap = term->alt_wrap;
697 term->alt_wrap = t;
698 t = term->wrapnext;
699 if (!reset) term->wrapnext = term->alt_wnext;
700 term->alt_wnext = t;
701 t = term->insert;
702 if (!reset) term->insert = term->alt_ins;
703 term->alt_ins = t;
704 t = term->cset;
705 if (!reset) term->cset = term->alt_cset;
706 term->alt_cset = t;
707 t = term->utf;
708 if (!reset) term->utf = term->alt_utf;
709 term->alt_utf = t;
710 t = term->sco_acs;
711 if (!reset) term->sco_acs = term->alt_sco_acs;
712 term->alt_sco_acs = t;
713 }
714
715 if (reset && term->screen) {
716 /*
717 * Yes, this _is_ supposed to honour background-colour-erase.
718 */
719 erase_lots(term, FALSE, TRUE, TRUE);
720 }
721
722 /*
723 * This might not be possible if we're called during
724 * initialisation.
725 */
726 if (term->screen)
727 fix_cpos;
728 }
729
730 /*
731 * Update the scroll bar.
732 */
733 static void update_sbar(Terminal *term)
734 {
735 int nscroll = sblines(term);
736 set_sbar(term->frontend, nscroll + term->rows,
737 nscroll + term->disptop, term->rows);
738 }
739
740 /*
741 * Check whether the region bounded by the two pointers intersects
742 * the scroll region, and de-select the on-screen selection if so.
743 */
744 static void check_selection(Terminal *term, pos from, pos to)
745 {
746 if (poslt(from, term->selend) && poslt(term->selstart, to))
747 deselect(term);
748 }
749
750 /*
751 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
752 * for backward.) `sb' is TRUE if the scrolling is permitted to
753 * affect the scrollback buffer.
754 *
755 * NB this function invalidates all pointers into lines of the
756 * screen data structures. In particular, you MUST call fix_cpos
757 * after calling scroll() and before doing anything else that
758 * uses the cpos shortcut pointer.
759 */
760 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
761 {
762 unsigned long *line, *line2;
763 int i, seltop, olddisptop, shift;
764
765 if (topline != 0 || term->alt_which != 0)
766 sb = FALSE;
767
768 olddisptop = term->disptop;
769 shift = lines;
770 if (lines < 0) {
771 while (lines < 0) {
772 line = delpos234(term->screen, botline);
773 line = resizeline(line, term->cols);
774 for (i = 0; i < term->cols; i++)
775 line[i + 1] = term->erase_char;
776 line[term->cols + 1] = 0;
777 addpos234(term->screen, line, topline);
778
779 if (term->selstart.y >= topline && term->selstart.y <= botline) {
780 term->selstart.y++;
781 if (term->selstart.y > botline) {
782 term->selstart.y = botline + 1;
783 term->selstart.x = 0;
784 }
785 }
786 if (term->selend.y >= topline && term->selend.y <= botline) {
787 term->selend.y++;
788 if (term->selend.y > botline) {
789 term->selend.y = botline + 1;
790 term->selend.x = 0;
791 }
792 }
793
794 lines++;
795 }
796 } else {
797 while (lines > 0) {
798 line = delpos234(term->screen, topline);
799 if (sb && term->savelines > 0) {
800 int sblen = count234(term->scrollback);
801 /*
802 * We must add this line to the scrollback. We'll
803 * remove a line from the top of the scrollback to
804 * replace it, or allocate a new one if the
805 * scrollback isn't full.
806 */
807 if (sblen == term->savelines) {
808 sblen--, line2 = delpos234(term->scrollback, 0);
809 } else {
810 line2 = snewn(term->cols + 2, TTYPE);
811 line2[0] = term->cols;
812 term->tempsblines += 1;
813 }
814 addpos234(term->scrollback, line, sblen);
815 line = line2;
816
817 /*
818 * If the user is currently looking at part of the
819 * scrollback, and they haven't enabled any options
820 * that are going to reset the scrollback as a
821 * result of this movement, then the chances are
822 * they'd like to keep looking at the same line. So
823 * we move their viewpoint at the same rate as the
824 * scroll, at least until their viewpoint hits the
825 * top end of the scrollback buffer, at which point
826 * we don't have the choice any more.
827 *
828 * Thanks to Jan Holmen Holsten for the idea and
829 * initial implementation.
830 */
831 if (term->disptop > -term->savelines && term->disptop < 0)
832 term->disptop--;
833 }
834 line = resizeline(line, term->cols);
835 for (i = 0; i < term->cols; i++)
836 line[i + 1] = term->erase_char;
837 line[term->cols + 1] = LATTR_NORM;
838 addpos234(term->screen, line, botline);
839
840 /*
841 * If the selection endpoints move into the scrollback,
842 * we keep them moving until they hit the top. However,
843 * of course, if the line _hasn't_ moved into the
844 * scrollback then we don't do this, and cut them off
845 * at the top of the scroll region.
846 *
847 * This applies to selstart and selend (for an existing
848 * selection), and also selanchor (for one being
849 * selected as we speak).
850 */
851 seltop = sb ? -term->savelines : topline;
852
853 if (term->selstate != NO_SELECTION) {
854 if (term->selstart.y >= seltop &&
855 term->selstart.y <= botline) {
856 term->selstart.y--;
857 if (term->selstart.y < seltop) {
858 term->selstart.y = seltop;
859 term->selstart.x = 0;
860 }
861 }
862 if (term->selend.y >= seltop && term->selend.y <= botline) {
863 term->selend.y--;
864 if (term->selend.y < seltop) {
865 term->selend.y = seltop;
866 term->selend.x = 0;
867 }
868 }
869 if (term->selanchor.y >= seltop &&
870 term->selanchor.y <= botline) {
871 term->selanchor.y--;
872 if (term->selanchor.y < seltop) {
873 term->selanchor.y = seltop;
874 term->selanchor.x = 0;
875 }
876 }
877 }
878
879 lines--;
880 }
881 }
882 #ifdef OPTIMISE_SCROLL
883 shift += term->disptop - olddisptop;
884 if (shift < term->rows && shift > -term->rows && shift != 0)
885 scroll_display(term, topline, botline, shift);
886 #endif /* OPTIMISE_SCROLL */
887 }
888
889 #ifdef OPTIMISE_SCROLL
890 /*
891 * Add a scroll of a region on the screen into the pending scroll list.
892 * `lines' is +ve for scrolling forward, -ve for backward.
893 *
894 * If the scroll is on the same area as the last scroll in the list,
895 * merge them.
896 */
897 static void save_scroll(Terminal *term, int topline, int botline, int lines)
898 {
899 struct scrollregion *newscroll;
900 if (term->scrolltail &&
901 term->scrolltail->topline == topline &&
902 term->scrolltail->botline == botline) {
903 term->scrolltail->lines += lines;
904 } else {
905 newscroll = snew(struct scrollregion);
906 newscroll->topline = topline;
907 newscroll->botline = botline;
908 newscroll->lines = lines;
909 newscroll->next = NULL;
910
911 if (!term->scrollhead)
912 term->scrollhead = newscroll;
913 else
914 term->scrolltail->next = newscroll;
915 term->scrolltail = newscroll;
916 }
917 }
918
919 /*
920 * Scroll the physical display, and our conception of it in disptext.
921 */
922 static void scroll_display(Terminal *term, int topline, int botline, int lines)
923 {
924 unsigned long *start, *end;
925 int distance, size, i;
926
927 start = term->disptext + topline * (term->cols + 1);
928 end = term->disptext + (botline + 1) * (term->cols + 1);
929 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
930 size = end - start - distance;
931 if (lines > 0) {
932 memmove(start, start + distance, size * TSIZE);
933 if (term->dispcurs >= start + distance &&
934 term->dispcurs <= start + distance + size)
935 term->dispcurs -= distance;
936 for (i = 0; i < distance; i++)
937 (start + size)[i] |= ATTR_INVALID;
938 } else {
939 memmove(start + distance, start, size * TSIZE);
940 if (term->dispcurs >= start && term->dispcurs <= start + size)
941 term->dispcurs += distance;
942 for (i = 0; i < distance; i++)
943 start[i] |= ATTR_INVALID;
944 }
945 save_scroll(term, topline, botline, lines);
946 }
947 #endif /* OPTIMISE_SCROLL */
948
949 /*
950 * Move the cursor to a given position, clipping at boundaries. We
951 * may or may not want to clip at the scroll margin: marg_clip is 0
952 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
953 * even _being_ outside the margins.
954 */
955 static void move(Terminal *term, int x, int y, int marg_clip)
956 {
957 if (x < 0)
958 x = 0;
959 if (x >= term->cols)
960 x = term->cols - 1;
961 if (marg_clip) {
962 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
963 y < term->marg_t)
964 y = term->marg_t;
965 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
966 y > term->marg_b)
967 y = term->marg_b;
968 }
969 if (y < 0)
970 y = 0;
971 if (y >= term->rows)
972 y = term->rows - 1;
973 term->curs.x = x;
974 term->curs.y = y;
975 fix_cpos;
976 term->wrapnext = FALSE;
977 }
978
979 /*
980 * Save or restore the cursor and SGR mode.
981 */
982 static void save_cursor(Terminal *term, int save)
983 {
984 if (save) {
985 term->savecurs = term->curs;
986 term->save_attr = term->curr_attr;
987 term->save_cset = term->cset;
988 term->save_utf = term->utf;
989 term->save_wnext = term->wrapnext;
990 term->save_csattr = term->cset_attr[term->cset];
991 term->save_sco_acs = term->sco_acs;
992 } else {
993 term->curs = term->savecurs;
994 /* Make sure the window hasn't shrunk since the save */
995 if (term->curs.x >= term->cols)
996 term->curs.x = term->cols - 1;
997 if (term->curs.y >= term->rows)
998 term->curs.y = term->rows - 1;
999
1000 term->curr_attr = term->save_attr;
1001 term->cset = term->save_cset;
1002 term->utf = term->save_utf;
1003 term->wrapnext = term->save_wnext;
1004 /*
1005 * wrapnext might reset to False if the x position is no
1006 * longer at the rightmost edge.
1007 */
1008 if (term->wrapnext && term->curs.x < term->cols-1)
1009 term->wrapnext = FALSE;
1010 term->cset_attr[term->cset] = term->save_csattr;
1011 term->sco_acs = term->save_sco_acs;
1012 fix_cpos;
1013 if (term->use_bce)
1014 term->erase_char = (' ' | ATTR_ASCII |
1015 (term->curr_attr &
1016 (ATTR_FGMASK | ATTR_BGMASK)));
1017 }
1018 }
1019
1020 /*
1021 * This function is called before doing _anything_ which affects
1022 * only part of a line of text. It is used to mark the boundary
1023 * between two character positions, and it indicates that some sort
1024 * of effect is going to happen on only one side of that boundary.
1025 *
1026 * The effect of this function is to check whether a CJK
1027 * double-width character is straddling the boundary, and to remove
1028 * it and replace it with two spaces if so. (Of course, one or
1029 * other of those spaces is then likely to be replaced with
1030 * something else again, as a result of whatever happens next.)
1031 *
1032 * Also, if the boundary is at the right-hand _edge_ of the screen,
1033 * it implies something deliberate is being done to the rightmost
1034 * column position; hence we must clear LATTR_WRAPPED2.
1035 *
1036 * The input to the function is the coordinates of the _second_
1037 * character of the pair.
1038 */
1039 static void check_boundary(Terminal *term, int x, int y)
1040 {
1041 unsigned long *ldata;
1042
1043 /* Validate input coordinates, just in case. */
1044 if (x == 0 || x > term->cols)
1045 return;
1046
1047 ldata = lineptr(y);
1048 if (x == term->cols) {
1049 ldata[x] &= ~LATTR_WRAPPED2;
1050 } else {
1051 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
1052 ldata[x-1] = ldata[x] =
1053 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
1054 }
1055 }
1056 }
1057
1058 /*
1059 * Erase a large portion of the screen: the whole screen, or the
1060 * whole line, or parts thereof.
1061 */
1062 static void erase_lots(Terminal *term,
1063 int line_only, int from_begin, int to_end)
1064 {
1065 pos start, end;
1066 int erase_lattr;
1067 int erasing_lines_from_top = 0;
1068
1069 if (line_only) {
1070 start.y = term->curs.y;
1071 start.x = 0;
1072 end.y = term->curs.y + 1;
1073 end.x = 0;
1074 erase_lattr = FALSE;
1075 } else {
1076 start.y = 0;
1077 start.x = 0;
1078 end.y = term->rows;
1079 end.x = 0;
1080 erase_lattr = TRUE;
1081 }
1082 if (!from_begin) {
1083 start = term->curs;
1084 }
1085 if (!to_end) {
1086 end = term->curs;
1087 incpos(end);
1088 }
1089 if (!from_begin || !to_end)
1090 check_boundary(term, term->curs.x, term->curs.y);
1091 check_selection(term, start, end);
1092
1093 /* Clear screen also forces a full window redraw, just in case. */
1094 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1095 term_invalidate(term);
1096
1097 /* Lines scrolled away shouldn't be brought back on if the terminal
1098 * resizes. */
1099 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1100 erasing_lines_from_top = 1;
1101
1102 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1103 /* If it's a whole number of lines, starting at the top, and
1104 * we're fully erasing them, erase by scrolling and keep the
1105 * lines in the scrollback. */
1106 int scrolllines = end.y;
1107 if (end.y == term->rows) {
1108 /* Shrink until we find a non-empty row.*/
1109 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1110 }
1111 if (scrolllines > 0)
1112 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1113 fix_cpos;
1114 } else {
1115 unsigned long *ldata = lineptr(start.y);
1116 while (poslt(start, end)) {
1117 if (start.x == term->cols) {
1118 if (!erase_lattr)
1119 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1120 else
1121 ldata[start.x] = LATTR_NORM;
1122 } else {
1123 ldata[start.x] = term->erase_char;
1124 }
1125 if (incpos(start) && start.y < term->rows)
1126 ldata = lineptr(start.y);
1127 }
1128 }
1129
1130 /* After an erase of lines from the top of the screen, we shouldn't
1131 * bring the lines back again if the terminal enlarges (since the user or
1132 * application has explictly thrown them away). */
1133 if (erasing_lines_from_top && !(term->alt_which))
1134 term->tempsblines = 0;
1135 }
1136
1137 /*
1138 * Insert or delete characters within the current line. n is +ve if
1139 * insertion is desired, and -ve for deletion.
1140 */
1141 static void insch(Terminal *term, int n)
1142 {
1143 int dir = (n < 0 ? -1 : +1);
1144 int m;
1145 pos cursplus;
1146 unsigned long *ldata;
1147
1148 n = (n < 0 ? -n : n);
1149 if (n > term->cols - term->curs.x)
1150 n = term->cols - term->curs.x;
1151 m = term->cols - term->curs.x - n;
1152 cursplus.y = term->curs.y;
1153 cursplus.x = term->curs.x + n;
1154 check_selection(term, term->curs, cursplus);
1155 check_boundary(term, term->curs.x, term->curs.y);
1156 if (dir < 0)
1157 check_boundary(term, term->curs.x + n, term->curs.y);
1158 ldata = lineptr(term->curs.y);
1159 if (dir < 0) {
1160 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1161 while (n--)
1162 ldata[term->curs.x + m++] = term->erase_char;
1163 } else {
1164 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1165 while (n--)
1166 ldata[term->curs.x + n] = term->erase_char;
1167 }
1168 }
1169
1170 /*
1171 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1172 * whether the mode is a DEC private one or a normal one.)
1173 */
1174 static void toggle_mode(Terminal *term, int mode, int query, int state)
1175 {
1176 unsigned long ticks;
1177
1178 if (query)
1179 switch (mode) {
1180 case 1: /* DECCKM: application cursor keys */
1181 term->app_cursor_keys = state;
1182 break;
1183 case 2: /* DECANM: VT52 mode */
1184 term->vt52_mode = !state;
1185 if (term->vt52_mode) {
1186 term->blink_is_real = FALSE;
1187 term->vt52_bold = FALSE;
1188 } else {
1189 term->blink_is_real = term->cfg.blinktext;
1190 }
1191 break;
1192 case 3: /* DECCOLM: 80/132 columns */
1193 deselect(term);
1194 if (!term->cfg.no_remote_resize)
1195 request_resize(term->frontend, state ? 132 : 80, term->rows);
1196 term->reset_132 = state;
1197 term->alt_t = term->marg_t = 0;
1198 term->alt_b = term->marg_b = term->rows - 1;
1199 move(term, 0, 0, 0);
1200 erase_lots(term, FALSE, TRUE, TRUE);
1201 break;
1202 case 5: /* DECSCNM: reverse video */
1203 /*
1204 * Toggle reverse video. If we receive an OFF within the
1205 * visual bell timeout period after an ON, we trigger an
1206 * effective visual bell, so that ESC[?5hESC[?5l will
1207 * always be an actually _visible_ visual bell.
1208 */
1209 ticks = GETTICKCOUNT();
1210 /* turn off a previous vbell to avoid inconsistencies */
1211 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1212 term->in_vbell = FALSE;
1213 if (term->rvideo && !state && /* we're turning it off... */
1214 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1215 /* If there's no vbell timeout already, or this one lasts
1216 * longer, replace vbell_timeout with ours. */
1217 if (!term->in_vbell ||
1218 (term->rvbell_startpoint - term->vbell_startpoint <
1219 VBELL_TIMEOUT))
1220 term->vbell_startpoint = term->rvbell_startpoint;
1221 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1222 } else if (!term->rvideo && state) {
1223 /* This is an ON, so we notice the time and save it. */
1224 term->rvbell_startpoint = ticks;
1225 }
1226 term->rvideo = state;
1227 term->seen_disp_event = TRUE;
1228 if (state)
1229 term_update(term);
1230 break;
1231 case 6: /* DECOM: DEC origin mode */
1232 term->dec_om = state;
1233 break;
1234 case 7: /* DECAWM: auto wrap */
1235 term->wrap = state;
1236 break;
1237 case 8: /* DECARM: auto key repeat */
1238 term->repeat_off = !state;
1239 break;
1240 case 10: /* DECEDM: set local edit mode */
1241 term->term_editing = state;
1242 if (term->ldisc) /* cause ldisc to notice changes */
1243 ldisc_send(term->ldisc, NULL, 0, 0);
1244 break;
1245 case 25: /* DECTCEM: enable/disable cursor */
1246 compatibility2(OTHER, VT220);
1247 term->cursor_on = state;
1248 term->seen_disp_event = TRUE;
1249 break;
1250 case 47: /* alternate screen */
1251 compatibility(OTHER);
1252 deselect(term);
1253 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1254 term->disptop = 0;
1255 break;
1256 case 1000: /* xterm mouse 1 */
1257 term->xterm_mouse = state ? 1 : 0;
1258 set_raw_mouse_mode(term->frontend, state);
1259 break;
1260 case 1002: /* xterm mouse 2 */
1261 term->xterm_mouse = state ? 2 : 0;
1262 set_raw_mouse_mode(term->frontend, state);
1263 break;
1264 case 1047: /* alternate screen */
1265 compatibility(OTHER);
1266 deselect(term);
1267 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1268 term->disptop = 0;
1269 break;
1270 case 1048: /* save/restore cursor */
1271 if (!term->cfg.no_alt_screen)
1272 save_cursor(term, state);
1273 if (!state) term->seen_disp_event = TRUE;
1274 break;
1275 case 1049: /* cursor & alternate screen */
1276 if (state && !term->cfg.no_alt_screen)
1277 save_cursor(term, state);
1278 if (!state) term->seen_disp_event = TRUE;
1279 compatibility(OTHER);
1280 deselect(term);
1281 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1282 if (!state && !term->cfg.no_alt_screen)
1283 save_cursor(term, state);
1284 term->disptop = 0;
1285 break;
1286 } else
1287 switch (mode) {
1288 case 4: /* IRM: set insert mode */
1289 compatibility(VT102);
1290 term->insert = state;
1291 break;
1292 case 12: /* SRM: set echo mode */
1293 term->term_echoing = !state;
1294 if (term->ldisc) /* cause ldisc to notice changes */
1295 ldisc_send(term->ldisc, NULL, 0, 0);
1296 break;
1297 case 20: /* LNM: Return sends ... */
1298 term->cr_lf_return = state;
1299 break;
1300 case 34: /* WYULCURM: Make cursor BIG */
1301 compatibility2(OTHER, VT220);
1302 term->big_cursor = !state;
1303 }
1304 }
1305
1306 /*
1307 * Process an OSC sequence: set window title or icon name.
1308 */
1309 static void do_osc(Terminal *term)
1310 {
1311 if (term->osc_w) {
1312 while (term->osc_strlen--)
1313 term->wordness[(unsigned char)
1314 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1315 } else {
1316 term->osc_string[term->osc_strlen] = '\0';
1317 switch (term->esc_args[0]) {
1318 case 0:
1319 case 1:
1320 if (!term->cfg.no_remote_wintitle)
1321 set_icon(term->frontend, term->osc_string);
1322 if (term->esc_args[0] == 1)
1323 break;
1324 /* fall through: parameter 0 means set both */
1325 case 2:
1326 case 21:
1327 if (!term->cfg.no_remote_wintitle)
1328 set_title(term->frontend, term->osc_string);
1329 break;
1330 }
1331 }
1332 }
1333
1334 /*
1335 * ANSI printing routines.
1336 */
1337 static void term_print_setup(Terminal *term)
1338 {
1339 bufchain_clear(&term->printer_buf);
1340 term->print_job = printer_start_job(term->cfg.printer);
1341 }
1342 static void term_print_flush(Terminal *term)
1343 {
1344 void *data;
1345 int len;
1346 int size;
1347 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1348 bufchain_prefix(&term->printer_buf, &data, &len);
1349 if (len > size-5)
1350 len = size-5;
1351 printer_job_data(term->print_job, data, len);
1352 bufchain_consume(&term->printer_buf, len);
1353 }
1354 }
1355 static void term_print_finish(Terminal *term)
1356 {
1357 void *data;
1358 int len, size;
1359 char c;
1360
1361 if (!term->printing && !term->only_printing)
1362 return; /* we need do nothing */
1363
1364 term_print_flush(term);
1365 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1366 bufchain_prefix(&term->printer_buf, &data, &len);
1367 c = *(char *)data;
1368 if (c == '\033' || c == '\233') {
1369 bufchain_consume(&term->printer_buf, size);
1370 break;
1371 } else {
1372 printer_job_data(term->print_job, &c, 1);
1373 bufchain_consume(&term->printer_buf, 1);
1374 }
1375 }
1376 printer_finish_job(term->print_job);
1377 term->print_job = NULL;
1378 term->printing = term->only_printing = FALSE;
1379 }
1380
1381 /*
1382 * Remove everything currently in `inbuf' and stick it up on the
1383 * in-memory display. There's a big state machine in here to
1384 * process escape sequences...
1385 */
1386 void term_out(Terminal *term)
1387 {
1388 int c, unget;
1389 unsigned char localbuf[256], *chars;
1390 int nchars = 0;
1391
1392 unget = -1;
1393
1394 chars = NULL; /* placate compiler warnings */
1395 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1396 if (unget == -1) {
1397 if (nchars == 0) {
1398 void *ret;
1399 bufchain_prefix(&term->inbuf, &ret, &nchars);
1400 if (nchars > sizeof(localbuf))
1401 nchars = sizeof(localbuf);
1402 memcpy(localbuf, ret, nchars);
1403 bufchain_consume(&term->inbuf, nchars);
1404 chars = localbuf;
1405 assert(chars != NULL);
1406 }
1407 c = *chars++;
1408 nchars--;
1409
1410 /*
1411 * Optionally log the session traffic to a file. Useful for
1412 * debugging and possibly also useful for actual logging.
1413 */
1414 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1415 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1416 } else {
1417 c = unget;
1418 unget = -1;
1419 }
1420
1421 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1422 * be able to display 8-bit characters, but I'll let that go 'cause
1423 * of i18n.
1424 */
1425
1426 /*
1427 * If we're printing, add the character to the printer
1428 * buffer.
1429 */
1430 if (term->printing) {
1431 bufchain_add(&term->printer_buf, &c, 1);
1432
1433 /*
1434 * If we're in print-only mode, we use a much simpler
1435 * state machine designed only to recognise the ESC[4i
1436 * termination sequence.
1437 */
1438 if (term->only_printing) {
1439 if (c == '\033')
1440 term->print_state = 1;
1441 else if (c == (unsigned char)'\233')
1442 term->print_state = 2;
1443 else if (c == '[' && term->print_state == 1)
1444 term->print_state = 2;
1445 else if (c == '4' && term->print_state == 2)
1446 term->print_state = 3;
1447 else if (c == 'i' && term->print_state == 3)
1448 term->print_state = 4;
1449 else
1450 term->print_state = 0;
1451 if (term->print_state == 4) {
1452 term_print_finish(term);
1453 }
1454 continue;
1455 }
1456 }
1457
1458 /* First see about all those translations. */
1459 if (term->termstate == TOPLEVEL) {
1460 if (in_utf(term))
1461 switch (term->utf_state) {
1462 case 0:
1463 if (c < 0x80) {
1464 /* UTF-8 must be stateless so we ignore iso2022. */
1465 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1466 c = term->ucsdata->unitab_ctrl[c];
1467 else c = ((unsigned char)c) | ATTR_ASCII;
1468 break;
1469 } else if ((c & 0xe0) == 0xc0) {
1470 term->utf_size = term->utf_state = 1;
1471 term->utf_char = (c & 0x1f);
1472 } else if ((c & 0xf0) == 0xe0) {
1473 term->utf_size = term->utf_state = 2;
1474 term->utf_char = (c & 0x0f);
1475 } else if ((c & 0xf8) == 0xf0) {
1476 term->utf_size = term->utf_state = 3;
1477 term->utf_char = (c & 0x07);
1478 } else if ((c & 0xfc) == 0xf8) {
1479 term->utf_size = term->utf_state = 4;
1480 term->utf_char = (c & 0x03);
1481 } else if ((c & 0xfe) == 0xfc) {
1482 term->utf_size = term->utf_state = 5;
1483 term->utf_char = (c & 0x01);
1484 } else {
1485 c = UCSERR;
1486 break;
1487 }
1488 continue;
1489 case 1:
1490 case 2:
1491 case 3:
1492 case 4:
1493 case 5:
1494 if ((c & 0xC0) != 0x80) {
1495 unget = c;
1496 c = UCSERR;
1497 term->utf_state = 0;
1498 break;
1499 }
1500 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1501 if (--term->utf_state)
1502 continue;
1503
1504 c = term->utf_char;
1505
1506 /* Is somebody trying to be evil! */
1507 if (c < 0x80 ||
1508 (c < 0x800 && term->utf_size >= 2) ||
1509 (c < 0x10000 && term->utf_size >= 3) ||
1510 (c < 0x200000 && term->utf_size >= 4) ||
1511 (c < 0x4000000 && term->utf_size >= 5))
1512 c = UCSERR;
1513
1514 /* Unicode line separator and paragraph separator are CR-LF */
1515 if (c == 0x2028 || c == 0x2029)
1516 c = 0x85;
1517
1518 /* High controls are probably a Baaad idea too. */
1519 if (c < 0xA0)
1520 c = 0xFFFD;
1521
1522 /* The UTF-16 surrogates are not nice either. */
1523 /* The standard give the option of decoding these:
1524 * I don't want to! */
1525 if (c >= 0xD800 && c < 0xE000)
1526 c = UCSERR;
1527
1528 /* ISO 10646 characters now limited to UTF-16 range. */
1529 if (c > 0x10FFFF)
1530 c = UCSERR;
1531
1532 /* This is currently a TagPhobic application.. */
1533 if (c >= 0xE0000 && c <= 0xE007F)
1534 continue;
1535
1536 /* U+FEFF is best seen as a null. */
1537 if (c == 0xFEFF)
1538 continue;
1539 /* But U+FFFE is an error. */
1540 if (c == 0xFFFE || c == 0xFFFF)
1541 c = UCSERR;
1542
1543 /* Oops this is a 16bit implementation */
1544 if (c >= 0x10000)
1545 c = 0xFFFD;
1546 break;
1547 }
1548 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1549 else if(term->sco_acs &&
1550 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1551 {
1552 if (term->sco_acs == 2) c |= 0x80;
1553 c |= ATTR_SCOACS;
1554 } else {
1555 switch (term->cset_attr[term->cset]) {
1556 /*
1557 * Linedraw characters are different from 'ESC ( B'
1558 * only for a small range. For ones outside that
1559 * range, make sure we use the same font as well as
1560 * the same encoding.
1561 */
1562 case ATTR_LINEDRW:
1563 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1564 c = term->ucsdata->unitab_ctrl[c];
1565 else
1566 c = ((unsigned char) c) | ATTR_LINEDRW;
1567 break;
1568
1569 case ATTR_GBCHR:
1570 /* If UK-ASCII, make the '#' a LineDraw Pound */
1571 if (c == '#') {
1572 c = '}' | ATTR_LINEDRW;
1573 break;
1574 }
1575 /*FALLTHROUGH*/ case ATTR_ASCII:
1576 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1577 c = term->ucsdata->unitab_ctrl[c];
1578 else
1579 c = ((unsigned char) c) | ATTR_ASCII;
1580 break;
1581 case ATTR_SCOACS:
1582 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1583 break;
1584 }
1585 }
1586 }
1587
1588 /* How about C1 controls ? */
1589 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1590 !term->vt52_mode && has_compat(VT220)) {
1591 term->termstate = SEEN_ESC;
1592 term->esc_query = FALSE;
1593 c = '@' + (c & 0x1F);
1594 }
1595
1596 /* Or the GL control. */
1597 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1598 if (term->curs.x && !term->wrapnext)
1599 term->curs.x--;
1600 term->wrapnext = FALSE;
1601 fix_cpos;
1602 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1603 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1604 } else
1605 /* Or normal C0 controls. */
1606 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1607 switch (c) {
1608 case '\005': /* ENQ: terminal type query */
1609 /* Strictly speaking this is VT100 but a VT100 defaults to
1610 * no response. Other terminals respond at their option.
1611 *
1612 * Don't put a CR in the default string as this tends to
1613 * upset some weird software.
1614 *
1615 * An xterm returns "xterm" (5 characters)
1616 */
1617 compatibility(ANSIMIN);
1618 if (term->ldisc) {
1619 char abuf[256], *s, *d;
1620 int state = 0;
1621 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1622 if (state) {
1623 if (*s >= 'a' && *s <= 'z')
1624 *d++ = (*s - ('a' - 1));
1625 else if ((*s >= '@' && *s <= '_') ||
1626 *s == '?' || (*s & 0x80))
1627 *d++ = ('@' ^ *s);
1628 else if (*s == '~')
1629 *d++ = '^';
1630 state = 0;
1631 } else if (*s == '^') {
1632 state = 1;
1633 } else
1634 *d++ = *s;
1635 }
1636 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1637 abuf, d - abuf, 0);
1638 }
1639 break;
1640 case '\007': /* BEL: Bell */
1641 {
1642 struct beeptime *newbeep;
1643 unsigned long ticks;
1644
1645 ticks = GETTICKCOUNT();
1646
1647 if (!term->beep_overloaded) {
1648 newbeep = snew(struct beeptime);
1649 newbeep->ticks = ticks;
1650 newbeep->next = NULL;
1651 if (!term->beephead)
1652 term->beephead = newbeep;
1653 else
1654 term->beeptail->next = newbeep;
1655 term->beeptail = newbeep;
1656 term->nbeeps++;
1657 }
1658
1659 /*
1660 * Throw out any beeps that happened more than
1661 * t seconds ago.
1662 */
1663 while (term->beephead &&
1664 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1665 struct beeptime *tmp = term->beephead;
1666 term->beephead = tmp->next;
1667 sfree(tmp);
1668 if (!term->beephead)
1669 term->beeptail = NULL;
1670 term->nbeeps--;
1671 }
1672
1673 if (term->cfg.bellovl && term->beep_overloaded &&
1674 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1675 /*
1676 * If we're currently overloaded and the
1677 * last beep was more than s seconds ago,
1678 * leave overload mode.
1679 */
1680 term->beep_overloaded = FALSE;
1681 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1682 term->nbeeps >= term->cfg.bellovl_n) {
1683 /*
1684 * Now, if we have n or more beeps
1685 * remaining in the queue, go into overload
1686 * mode.
1687 */
1688 term->beep_overloaded = TRUE;
1689 }
1690 term->lastbeep = ticks;
1691
1692 /*
1693 * Perform an actual beep if we're not overloaded.
1694 */
1695 if (!term->cfg.bellovl || !term->beep_overloaded) {
1696 beep(term->frontend, term->cfg.beep);
1697 if (term->cfg.beep == BELL_VISUAL) {
1698 term->in_vbell = TRUE;
1699 term->vbell_startpoint = ticks;
1700 term_update(term);
1701 }
1702 }
1703 term->seen_disp_event = TRUE;
1704 }
1705 break;
1706 case '\b': /* BS: Back space */
1707 if (term->curs.x == 0 &&
1708 (term->curs.y == 0 || term->wrap == 0))
1709 /* do nothing */ ;
1710 else if (term->curs.x == 0 && term->curs.y > 0)
1711 term->curs.x = term->cols - 1, term->curs.y--;
1712 else if (term->wrapnext)
1713 term->wrapnext = FALSE;
1714 else
1715 term->curs.x--;
1716 fix_cpos;
1717 term->seen_disp_event = TRUE;
1718 break;
1719 case '\016': /* LS1: Locking-shift one */
1720 compatibility(VT100);
1721 term->cset = 1;
1722 break;
1723 case '\017': /* LS0: Locking-shift zero */
1724 compatibility(VT100);
1725 term->cset = 0;
1726 break;
1727 case '\033': /* ESC: Escape */
1728 if (term->vt52_mode)
1729 term->termstate = VT52_ESC;
1730 else {
1731 compatibility(ANSIMIN);
1732 term->termstate = SEEN_ESC;
1733 term->esc_query = FALSE;
1734 }
1735 break;
1736 case '\015': /* CR: Carriage return */
1737 term->curs.x = 0;
1738 term->wrapnext = FALSE;
1739 fix_cpos;
1740 term->seen_disp_event = TRUE;
1741 term->paste_hold = 0;
1742 if (term->logctx)
1743 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1744 break;
1745 case '\014': /* FF: Form feed */
1746 if (has_compat(SCOANSI)) {
1747 move(term, 0, 0, 0);
1748 erase_lots(term, FALSE, FALSE, TRUE);
1749 term->disptop = 0;
1750 term->wrapnext = FALSE;
1751 term->seen_disp_event = 1;
1752 break;
1753 }
1754 case '\013': /* VT: Line tabulation */
1755 compatibility(VT100);
1756 case '\012': /* LF: Line feed */
1757 if (term->curs.y == term->marg_b)
1758 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1759 else if (term->curs.y < term->rows - 1)
1760 term->curs.y++;
1761 if (term->cfg.lfhascr)
1762 term->curs.x = 0;
1763 fix_cpos;
1764 term->wrapnext = FALSE;
1765 term->seen_disp_event = 1;
1766 term->paste_hold = 0;
1767 if (term->logctx)
1768 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1769 break;
1770 case '\t': /* HT: Character tabulation */
1771 {
1772 pos old_curs = term->curs;
1773 unsigned long *ldata = lineptr(term->curs.y);
1774
1775 do {
1776 term->curs.x++;
1777 } while (term->curs.x < term->cols - 1 &&
1778 !term->tabs[term->curs.x]);
1779
1780 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1781 if (term->curs.x >= term->cols / 2)
1782 term->curs.x = term->cols / 2 - 1;
1783 } else {
1784 if (term->curs.x >= term->cols)
1785 term->curs.x = term->cols - 1;
1786 }
1787
1788 fix_cpos;
1789 check_selection(term, old_curs, term->curs);
1790 }
1791 term->seen_disp_event = TRUE;
1792 break;
1793 }
1794 } else
1795 switch (term->termstate) {
1796 case TOPLEVEL:
1797 /* Only graphic characters get this far;
1798 * ctrls are stripped above */
1799 if (term->wrapnext && term->wrap) {
1800 term->cpos[1] |= LATTR_WRAPPED;
1801 if (term->curs.y == term->marg_b)
1802 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1803 else if (term->curs.y < term->rows - 1)
1804 term->curs.y++;
1805 term->curs.x = 0;
1806 fix_cpos;
1807 term->wrapnext = FALSE;
1808 }
1809 if (term->insert)
1810 insch(term, 1);
1811 if (term->selstate != NO_SELECTION) {
1812 pos cursplus = term->curs;
1813 incpos(cursplus);
1814 check_selection(term, term->curs, cursplus);
1815 }
1816 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1817 term->logctx)
1818 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1819 {
1820 int width = 0;
1821 if (DIRECT_CHAR(c))
1822 width = 1;
1823 if (!width)
1824 width = wcwidth((wchar_t) c);
1825 switch (width) {
1826 case 2:
1827 /*
1828 * If we're about to display a double-width
1829 * character starting in the rightmost
1830 * column, then we do something special
1831 * instead. We must print a space in the
1832 * last column of the screen, then wrap;
1833 * and we also set LATTR_WRAPPED2 which
1834 * instructs subsequent cut-and-pasting not
1835 * only to splice this line to the one
1836 * after it, but to ignore the space in the
1837 * last character position as well.
1838 * (Because what was actually output to the
1839 * terminal was presumably just a sequence
1840 * of CJK characters, and we don't want a
1841 * space to be pasted in the middle of
1842 * those just because they had the
1843 * misfortune to start in the wrong parity
1844 * column. xterm concurs.)
1845 */
1846 check_boundary(term, term->curs.x, term->curs.y);
1847 check_boundary(term, term->curs.x+2, term->curs.y);
1848 if (term->curs.x == term->cols-1) {
1849 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1850 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1851 if (term->curs.y == term->marg_b)
1852 scroll(term, term->marg_t, term->marg_b,
1853 1, TRUE);
1854 else if (term->curs.y < term->rows - 1)
1855 term->curs.y++;
1856 term->curs.x = 0;
1857 fix_cpos;
1858 /* Now we must check_boundary again, of course. */
1859 check_boundary(term, term->curs.x, term->curs.y);
1860 check_boundary(term, term->curs.x+2, term->curs.y);
1861 }
1862 *term->cpos++ = c | term->curr_attr;
1863 *term->cpos++ = UCSWIDE | term->curr_attr;
1864 term->curs.x++;
1865 break;
1866 case 1:
1867 check_boundary(term, term->curs.x, term->curs.y);
1868 check_boundary(term, term->curs.x+1, term->curs.y);
1869 *term->cpos++ = c | term->curr_attr;
1870 break;
1871 default:
1872 continue;
1873 }
1874 }
1875 term->curs.x++;
1876 if (term->curs.x == term->cols) {
1877 term->cpos--;
1878 term->curs.x--;
1879 term->wrapnext = TRUE;
1880 if (term->wrap && term->vt52_mode) {
1881 term->cpos[1] |= LATTR_WRAPPED;
1882 if (term->curs.y == term->marg_b)
1883 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1884 else if (term->curs.y < term->rows - 1)
1885 term->curs.y++;
1886 term->curs.x = 0;
1887 fix_cpos;
1888 term->wrapnext = FALSE;
1889 }
1890 }
1891 term->seen_disp_event = 1;
1892 break;
1893
1894 case OSC_MAYBE_ST:
1895 /*
1896 * This state is virtually identical to SEEN_ESC, with the
1897 * exception that we have an OSC sequence in the pipeline,
1898 * and _if_ we see a backslash, we process it.
1899 */
1900 if (c == '\\') {
1901 do_osc(term);
1902 term->termstate = TOPLEVEL;
1903 break;
1904 }
1905 /* else fall through */
1906 case SEEN_ESC:
1907 if (c >= ' ' && c <= '/') {
1908 if (term->esc_query)
1909 term->esc_query = -1;
1910 else
1911 term->esc_query = c;
1912 break;
1913 }
1914 term->termstate = TOPLEVEL;
1915 switch (ANSI(c, term->esc_query)) {
1916 case '[': /* enter CSI mode */
1917 term->termstate = SEEN_CSI;
1918 term->esc_nargs = 1;
1919 term->esc_args[0] = ARG_DEFAULT;
1920 term->esc_query = FALSE;
1921 break;
1922 case ']': /* OSC: xterm escape sequences */
1923 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1924 compatibility(OTHER);
1925 term->termstate = SEEN_OSC;
1926 term->esc_args[0] = 0;
1927 break;
1928 case '7': /* DECSC: save cursor */
1929 compatibility(VT100);
1930 save_cursor(term, TRUE);
1931 break;
1932 case '8': /* DECRC: restore cursor */
1933 compatibility(VT100);
1934 save_cursor(term, FALSE);
1935 term->seen_disp_event = TRUE;
1936 break;
1937 case '=': /* DECKPAM: Keypad application mode */
1938 compatibility(VT100);
1939 term->app_keypad_keys = TRUE;
1940 break;
1941 case '>': /* DECKPNM: Keypad numeric mode */
1942 compatibility(VT100);
1943 term->app_keypad_keys = FALSE;
1944 break;
1945 case 'D': /* IND: exactly equivalent to LF */
1946 compatibility(VT100);
1947 if (term->curs.y == term->marg_b)
1948 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1949 else if (term->curs.y < term->rows - 1)
1950 term->curs.y++;
1951 fix_cpos;
1952 term->wrapnext = FALSE;
1953 term->seen_disp_event = TRUE;
1954 break;
1955 case 'E': /* NEL: exactly equivalent to CR-LF */
1956 compatibility(VT100);
1957 term->curs.x = 0;
1958 if (term->curs.y == term->marg_b)
1959 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1960 else if (term->curs.y < term->rows - 1)
1961 term->curs.y++;
1962 fix_cpos;
1963 term->wrapnext = FALSE;
1964 term->seen_disp_event = TRUE;
1965 break;
1966 case 'M': /* RI: reverse index - backwards LF */
1967 compatibility(VT100);
1968 if (term->curs.y == term->marg_t)
1969 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1970 else if (term->curs.y > 0)
1971 term->curs.y--;
1972 fix_cpos;
1973 term->wrapnext = FALSE;
1974 term->seen_disp_event = TRUE;
1975 break;
1976 case 'Z': /* DECID: terminal type query */
1977 compatibility(VT100);
1978 if (term->ldisc)
1979 ldisc_send(term->ldisc, term->id_string,
1980 strlen(term->id_string), 0);
1981 break;
1982 case 'c': /* RIS: restore power-on settings */
1983 compatibility(VT100);
1984 power_on(term);
1985 if (term->ldisc) /* cause ldisc to notice changes */
1986 ldisc_send(term->ldisc, NULL, 0, 0);
1987 if (term->reset_132) {
1988 if (!term->cfg.no_remote_resize)
1989 request_resize(term->frontend, 80, term->rows);
1990 term->reset_132 = 0;
1991 }
1992 fix_cpos;
1993 term->disptop = 0;
1994 term->seen_disp_event = TRUE;
1995 break;
1996 case 'H': /* HTS: set a tab */
1997 compatibility(VT100);
1998 term->tabs[term->curs.x] = TRUE;
1999 break;
2000
2001 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2002 compatibility(VT100);
2003 {
2004 unsigned long *ldata;
2005 int i, j;
2006 pos scrtop, scrbot;
2007
2008 for (i = 0; i < term->rows; i++) {
2009 ldata = lineptr(i);
2010 for (j = 0; j < term->cols; j++)
2011 ldata[j] = ATTR_DEFAULT | 'E';
2012 ldata[term->cols] = 0;
2013 }
2014 term->disptop = 0;
2015 term->seen_disp_event = TRUE;
2016 scrtop.x = scrtop.y = 0;
2017 scrbot.x = 0;
2018 scrbot.y = term->rows;
2019 check_selection(term, scrtop, scrbot);
2020 }
2021 break;
2022
2023 case ANSI('3', '#'):
2024 case ANSI('4', '#'):
2025 case ANSI('5', '#'):
2026 case ANSI('6', '#'):
2027 compatibility(VT100);
2028 {
2029 unsigned long nlattr;
2030 unsigned long *ldata;
2031 switch (ANSI(c, term->esc_query)) {
2032 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2033 nlattr = LATTR_TOP;
2034 break;
2035 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2036 nlattr = LATTR_BOT;
2037 break;
2038 case ANSI('5', '#'): /* DECSWL: normal */
2039 nlattr = LATTR_NORM;
2040 break;
2041 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2042 nlattr = LATTR_WIDE;
2043 break;
2044 }
2045 ldata = lineptr(term->curs.y);
2046 ldata[term->cols] &= ~LATTR_MODE;
2047 ldata[term->cols] |= nlattr;
2048 }
2049 break;
2050 /* GZD4: G0 designate 94-set */
2051 case ANSI('A', '('):
2052 compatibility(VT100);
2053 if (!term->cfg.no_remote_charset)
2054 term->cset_attr[0] = ATTR_GBCHR;
2055 break;
2056 case ANSI('B', '('):
2057 compatibility(VT100);
2058 if (!term->cfg.no_remote_charset)
2059 term->cset_attr[0] = ATTR_ASCII;
2060 break;
2061 case ANSI('0', '('):
2062 compatibility(VT100);
2063 if (!term->cfg.no_remote_charset)
2064 term->cset_attr[0] = ATTR_LINEDRW;
2065 break;
2066 case ANSI('U', '('):
2067 compatibility(OTHER);
2068 if (!term->cfg.no_remote_charset)
2069 term->cset_attr[0] = ATTR_SCOACS;
2070 break;
2071 /* G1D4: G1-designate 94-set */
2072 case ANSI('A', ')'):
2073 compatibility(VT100);
2074 if (!term->cfg.no_remote_charset)
2075 term->cset_attr[1] = ATTR_GBCHR;
2076 break;
2077 case ANSI('B', ')'):
2078 compatibility(VT100);
2079 if (!term->cfg.no_remote_charset)
2080 term->cset_attr[1] = ATTR_ASCII;
2081 break;
2082 case ANSI('0', ')'):
2083 compatibility(VT100);
2084 if (!term->cfg.no_remote_charset)
2085 term->cset_attr[1] = ATTR_LINEDRW;
2086 break;
2087 case ANSI('U', ')'):
2088 compatibility(OTHER);
2089 if (!term->cfg.no_remote_charset)
2090 term->cset_attr[1] = ATTR_SCOACS;
2091 break;
2092 /* DOCS: Designate other coding system */
2093 case ANSI('8', '%'): /* Old Linux code */
2094 case ANSI('G', '%'):
2095 compatibility(OTHER);
2096 if (!term->cfg.no_remote_charset)
2097 term->utf = 1;
2098 break;
2099 case ANSI('@', '%'):
2100 compatibility(OTHER);
2101 if (!term->cfg.no_remote_charset)
2102 term->utf = 0;
2103 break;
2104 }
2105 break;
2106 case SEEN_CSI:
2107 term->termstate = TOPLEVEL; /* default */
2108 if (isdigit(c)) {
2109 if (term->esc_nargs <= ARGS_MAX) {
2110 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2111 term->esc_args[term->esc_nargs - 1] = 0;
2112 term->esc_args[term->esc_nargs - 1] =
2113 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2114 }
2115 term->termstate = SEEN_CSI;
2116 } else if (c == ';') {
2117 if (++term->esc_nargs <= ARGS_MAX)
2118 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2119 term->termstate = SEEN_CSI;
2120 } else if (c < '@') {
2121 if (term->esc_query)
2122 term->esc_query = -1;
2123 else if (c == '?')
2124 term->esc_query = TRUE;
2125 else
2126 term->esc_query = c;
2127 term->termstate = SEEN_CSI;
2128 } else
2129 switch (ANSI(c, term->esc_query)) {
2130 case 'A': /* CUU: move up N lines */
2131 move(term, term->curs.x,
2132 term->curs.y - def(term->esc_args[0], 1), 1);
2133 term->seen_disp_event = TRUE;
2134 break;
2135 case 'e': /* VPR: move down N lines */
2136 compatibility(ANSI);
2137 /* FALLTHROUGH */
2138 case 'B': /* CUD: Cursor down */
2139 move(term, term->curs.x,
2140 term->curs.y + def(term->esc_args[0], 1), 1);
2141 term->seen_disp_event = TRUE;
2142 break;
2143 case ANSI('c', '>'): /* DA: report xterm version */
2144 compatibility(OTHER);
2145 /* this reports xterm version 136 so that VIM can
2146 use the drag messages from the mouse reporting */
2147 if (term->ldisc)
2148 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2149 break;
2150 case 'a': /* HPR: move right N cols */
2151 compatibility(ANSI);
2152 /* FALLTHROUGH */
2153 case 'C': /* CUF: Cursor right */
2154 move(term, term->curs.x + def(term->esc_args[0], 1),
2155 term->curs.y, 1);
2156 term->seen_disp_event = TRUE;
2157 break;
2158 case 'D': /* CUB: move left N cols */
2159 move(term, term->curs.x - def(term->esc_args[0], 1),
2160 term->curs.y, 1);
2161 term->seen_disp_event = TRUE;
2162 break;
2163 case 'E': /* CNL: move down N lines and CR */
2164 compatibility(ANSI);
2165 move(term, 0,
2166 term->curs.y + def(term->esc_args[0], 1), 1);
2167 term->seen_disp_event = TRUE;
2168 break;
2169 case 'F': /* CPL: move up N lines and CR */
2170 compatibility(ANSI);
2171 move(term, 0,
2172 term->curs.y - def(term->esc_args[0], 1), 1);
2173 term->seen_disp_event = TRUE;
2174 break;
2175 case 'G': /* CHA */
2176 case '`': /* HPA: set horizontal posn */
2177 compatibility(ANSI);
2178 move(term, def(term->esc_args[0], 1) - 1,
2179 term->curs.y, 0);
2180 term->seen_disp_event = TRUE;
2181 break;
2182 case 'd': /* VPA: set vertical posn */
2183 compatibility(ANSI);
2184 move(term, term->curs.x,
2185 ((term->dec_om ? term->marg_t : 0) +
2186 def(term->esc_args[0], 1) - 1),
2187 (term->dec_om ? 2 : 0));
2188 term->seen_disp_event = TRUE;
2189 break;
2190 case 'H': /* CUP */
2191 case 'f': /* HVP: set horz and vert posns at once */
2192 if (term->esc_nargs < 2)
2193 term->esc_args[1] = ARG_DEFAULT;
2194 move(term, def(term->esc_args[1], 1) - 1,
2195 ((term->dec_om ? term->marg_t : 0) +
2196 def(term->esc_args[0], 1) - 1),
2197 (term->dec_om ? 2 : 0));
2198 term->seen_disp_event = TRUE;
2199 break;
2200 case 'J': /* ED: erase screen or parts of it */
2201 {
2202 unsigned int i = def(term->esc_args[0], 0) + 1;
2203 if (i > 3)
2204 i = 0;
2205 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2206 }
2207 term->disptop = 0;
2208 term->seen_disp_event = TRUE;
2209 break;
2210 case 'K': /* EL: erase line or parts of it */
2211 {
2212 unsigned int i = def(term->esc_args[0], 0) + 1;
2213 if (i > 3)
2214 i = 0;
2215 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2216 }
2217 term->seen_disp_event = TRUE;
2218 break;
2219 case 'L': /* IL: insert lines */
2220 compatibility(VT102);
2221 if (term->curs.y <= term->marg_b)
2222 scroll(term, term->curs.y, term->marg_b,
2223 -def(term->esc_args[0], 1), FALSE);
2224 fix_cpos;
2225 term->seen_disp_event = TRUE;
2226 break;
2227 case 'M': /* DL: delete lines */
2228 compatibility(VT102);
2229 if (term->curs.y <= term->marg_b)
2230 scroll(term, term->curs.y, term->marg_b,
2231 def(term->esc_args[0], 1),
2232 TRUE);
2233 fix_cpos;
2234 term->seen_disp_event = TRUE;
2235 break;
2236 case '@': /* ICH: insert chars */
2237 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2238 compatibility(VT102);
2239 insch(term, def(term->esc_args[0], 1));
2240 term->seen_disp_event = TRUE;
2241 break;
2242 case 'P': /* DCH: delete chars */
2243 compatibility(VT102);
2244 insch(term, -def(term->esc_args[0], 1));
2245 term->seen_disp_event = TRUE;
2246 break;
2247 case 'c': /* DA: terminal type query */
2248 compatibility(VT100);
2249 /* This is the response for a VT102 */
2250 if (term->ldisc)
2251 ldisc_send(term->ldisc, term->id_string,
2252 strlen(term->id_string), 0);
2253 break;
2254 case 'n': /* DSR: cursor position query */
2255 if (term->ldisc) {
2256 if (term->esc_args[0] == 6) {
2257 char buf[32];
2258 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2259 term->curs.x + 1);
2260 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2261 } else if (term->esc_args[0] == 5) {
2262 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2263 }
2264 }
2265 break;
2266 case 'h': /* SM: toggle modes to high */
2267 case ANSI_QUE('h'):
2268 compatibility(VT100);
2269 {
2270 int i;
2271 for (i = 0; i < term->esc_nargs; i++)
2272 toggle_mode(term, term->esc_args[i],
2273 term->esc_query, TRUE);
2274 }
2275 break;
2276 case 'i': /* MC: Media copy */
2277 case ANSI_QUE('i'):
2278 compatibility(VT100);
2279 {
2280 if (term->esc_nargs != 1) break;
2281 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2282 term->printing = TRUE;
2283 term->only_printing = !term->esc_query;
2284 term->print_state = 0;
2285 term_print_setup(term);
2286 } else if (term->esc_args[0] == 4 &&
2287 term->printing) {
2288 term_print_finish(term);
2289 }
2290 }
2291 break;
2292 case 'l': /* RM: toggle modes to low */
2293 case ANSI_QUE('l'):
2294 compatibility(VT100);
2295 {
2296 int i;
2297 for (i = 0; i < term->esc_nargs; i++)
2298 toggle_mode(term, term->esc_args[i],
2299 term->esc_query, FALSE);
2300 }
2301 break;
2302 case 'g': /* TBC: clear tabs */
2303 compatibility(VT100);
2304 if (term->esc_nargs == 1) {
2305 if (term->esc_args[0] == 0) {
2306 term->tabs[term->curs.x] = FALSE;
2307 } else if (term->esc_args[0] == 3) {
2308 int i;
2309 for (i = 0; i < term->cols; i++)
2310 term->tabs[i] = FALSE;
2311 }
2312 }
2313 break;
2314 case 'r': /* DECSTBM: set scroll margins */
2315 compatibility(VT100);
2316 if (term->esc_nargs <= 2) {
2317 int top, bot;
2318 top = def(term->esc_args[0], 1) - 1;
2319 bot = (term->esc_nargs <= 1
2320 || term->esc_args[1] == 0 ?
2321 term->rows :
2322 def(term->esc_args[1], term->rows)) - 1;
2323 if (bot >= term->rows)
2324 bot = term->rows - 1;
2325 /* VTTEST Bug 9 - if region is less than 2 lines
2326 * don't change region.
2327 */
2328 if (bot - top > 0) {
2329 term->marg_t = top;
2330 term->marg_b = bot;
2331 term->curs.x = 0;
2332 /*
2333 * I used to think the cursor should be
2334 * placed at the top of the newly marginned
2335 * area. Apparently not: VMS TPU falls over
2336 * if so.
2337 *
2338 * Well actually it should for
2339 * Origin mode - RDB
2340 */
2341 term->curs.y = (term->dec_om ?
2342 term->marg_t : 0);
2343 fix_cpos;
2344 term->seen_disp_event = TRUE;
2345 }
2346 }
2347 break;
2348 case 'm': /* SGR: set graphics rendition */
2349 {
2350 /*
2351 * A VT100 without the AVO only had one
2352 * attribute, either underline or
2353 * reverse video depending on the
2354 * cursor type, this was selected by
2355 * CSI 7m.
2356 *
2357 * case 2:
2358 * This is sometimes DIM, eg on the
2359 * GIGI and Linux
2360 * case 8:
2361 * This is sometimes INVIS various ANSI.
2362 * case 21:
2363 * This like 22 disables BOLD, DIM and INVIS
2364 *
2365 * The ANSI colours appear on any
2366 * terminal that has colour (obviously)
2367 * but the interaction between sgr0 and
2368 * the colours varies but is usually
2369 * related to the background colour
2370 * erase item. The interaction between
2371 * colour attributes and the mono ones
2372 * is also very implementation
2373 * dependent.
2374 *
2375 * The 39 and 49 attributes are likely
2376 * to be unimplemented.
2377 */
2378 int i;
2379 for (i = 0; i < term->esc_nargs; i++) {
2380 switch (def(term->esc_args[i], 0)) {
2381 case 0: /* restore defaults */
2382 term->curr_attr = term->default_attr;
2383 break;
2384 case 1: /* enable bold */
2385 compatibility(VT100AVO);
2386 term->curr_attr |= ATTR_BOLD;
2387 break;
2388 case 21: /* (enable double underline) */
2389 compatibility(OTHER);
2390 case 4: /* enable underline */
2391 compatibility(VT100AVO);
2392 term->curr_attr |= ATTR_UNDER;
2393 break;
2394 case 5: /* enable blink */
2395 compatibility(VT100AVO);
2396 term->curr_attr |= ATTR_BLINK;
2397 break;
2398 case 6: /* SCO light bkgrd */
2399 compatibility(SCOANSI);
2400 term->blink_is_real = FALSE;
2401 term->curr_attr |= ATTR_BLINK;
2402 break;
2403 case 7: /* enable reverse video */
2404 term->curr_attr |= ATTR_REVERSE;
2405 break;
2406 case 10: /* SCO acs off */
2407 compatibility(SCOANSI);
2408 if (term->cfg.no_remote_charset) break;
2409 term->sco_acs = 0; break;
2410 case 11: /* SCO acs on */
2411 compatibility(SCOANSI);
2412 if (term->cfg.no_remote_charset) break;
2413 term->sco_acs = 1; break;
2414 case 12: /* SCO acs on, |0x80 */
2415 compatibility(SCOANSI);
2416 if (term->cfg.no_remote_charset) break;
2417 term->sco_acs = 2; break;
2418 case 22: /* disable bold */
2419 compatibility2(OTHER, VT220);
2420 term->curr_attr &= ~ATTR_BOLD;
2421 break;
2422 case 24: /* disable underline */
2423 compatibility2(OTHER, VT220);
2424 term->curr_attr &= ~ATTR_UNDER;
2425 break;
2426 case 25: /* disable blink */
2427 compatibility2(OTHER, VT220);
2428 term->curr_attr &= ~ATTR_BLINK;
2429 break;
2430 case 27: /* disable reverse video */
2431 compatibility2(OTHER, VT220);
2432 term->curr_attr &= ~ATTR_REVERSE;
2433 break;
2434 case 30:
2435 case 31:
2436 case 32:
2437 case 33:
2438 case 34:
2439 case 35:
2440 case 36:
2441 case 37:
2442 /* foreground */
2443 term->curr_attr &= ~ATTR_FGMASK;
2444 term->curr_attr |=
2445 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2446 break;
2447 case 90:
2448 case 91:
2449 case 92:
2450 case 93:
2451 case 94:
2452 case 95:
2453 case 96:
2454 case 97:
2455 /* xterm-style bright foreground */
2456 term->curr_attr &= ~ATTR_FGMASK;
2457 term->curr_attr |=
2458 ((term->esc_args[i] - 90 + 16)
2459 << ATTR_FGSHIFT);
2460 break;
2461 case 39: /* default-foreground */
2462 term->curr_attr &= ~ATTR_FGMASK;
2463 term->curr_attr |= ATTR_DEFFG;
2464 break;
2465 case 40:
2466 case 41:
2467 case 42:
2468 case 43:
2469 case 44:
2470 case 45:
2471 case 46:
2472 case 47:
2473 /* background */
2474 term->curr_attr &= ~ATTR_BGMASK;
2475 term->curr_attr |=
2476 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2477 break;
2478 case 100:
2479 case 101:
2480 case 102:
2481 case 103:
2482 case 104:
2483 case 105:
2484 case 106:
2485 case 107:
2486 /* xterm-style bright background */
2487 term->curr_attr &= ~ATTR_BGMASK;
2488 term->curr_attr |=
2489 ((term->esc_args[i] - 100 + 16)
2490 << ATTR_BGSHIFT);
2491 break;
2492 case 49: /* default-background */
2493 term->curr_attr &= ~ATTR_BGMASK;
2494 term->curr_attr |= ATTR_DEFBG;
2495 break;
2496 }
2497 }
2498 if (term->use_bce)
2499 term->erase_char = (' ' | ATTR_ASCII |
2500 (term->curr_attr &
2501 (ATTR_FGMASK |
2502 ATTR_BGMASK)));
2503 }
2504 break;
2505 case 's': /* save cursor */
2506 save_cursor(term, TRUE);
2507 break;
2508 case 'u': /* restore cursor */
2509 save_cursor(term, FALSE);
2510 term->seen_disp_event = TRUE;
2511 break;
2512 case 't': /* DECSLPP: set page size - ie window height */
2513 /*
2514 * VT340/VT420 sequence DECSLPP, DEC only allows values
2515 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2516 * illegal values (eg first arg 1..9) for window changing
2517 * and reports.
2518 */
2519 if (term->esc_nargs <= 1
2520 && (term->esc_args[0] < 1 ||
2521 term->esc_args[0] >= 24)) {
2522 compatibility(VT340TEXT);
2523 if (!term->cfg.no_remote_resize)
2524 request_resize(term->frontend, term->cols,
2525 def(term->esc_args[0], 24));
2526 deselect(term);
2527 } else if (term->esc_nargs >= 1 &&
2528 term->esc_args[0] >= 1 &&
2529 term->esc_args[0] < 24) {
2530 compatibility(OTHER);
2531
2532 switch (term->esc_args[0]) {
2533 int x, y, len;
2534 char buf[80], *p;
2535 case 1:
2536 set_iconic(term->frontend, FALSE);
2537 break;
2538 case 2:
2539 set_iconic(term->frontend, TRUE);
2540 break;
2541 case 3:
2542 if (term->esc_nargs >= 3) {
2543 if (!term->cfg.no_remote_resize)
2544 move_window(term->frontend,
2545 def(term->esc_args[1], 0),
2546 def(term->esc_args[2], 0));
2547 }
2548 break;
2549 case 4:
2550 /* We should resize the window to a given
2551 * size in pixels here, but currently our
2552 * resizing code isn't healthy enough to
2553 * manage it. */
2554 break;
2555 case 5:
2556 /* move to top */
2557 set_zorder(term->frontend, TRUE);
2558 break;
2559 case 6:
2560 /* move to bottom */
2561 set_zorder(term->frontend, FALSE);
2562 break;
2563 case 7:
2564 refresh_window(term->frontend);
2565 break;
2566 case 8:
2567 if (term->esc_nargs >= 3) {
2568 if (!term->cfg.no_remote_resize)
2569 request_resize(term->frontend,
2570 def(term->esc_args[2], term->cfg.width),
2571 def(term->esc_args[1], term->cfg.height));
2572 }
2573 break;
2574 case 9:
2575 if (term->esc_nargs >= 2)
2576 set_zoomed(term->frontend,
2577 term->esc_args[1] ?
2578 TRUE : FALSE);
2579 break;
2580 case 11:
2581 if (term->ldisc)
2582 ldisc_send(term->ldisc,
2583 is_iconic(term->frontend) ?
2584 "\033[1t" : "\033[2t", 4, 0);
2585 break;
2586 case 13:
2587 if (term->ldisc) {
2588 get_window_pos(term->frontend, &x, &y);
2589 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2590 ldisc_send(term->ldisc, buf, len, 0);
2591 }
2592 break;
2593 case 14:
2594 if (term->ldisc) {
2595 get_window_pixels(term->frontend, &x, &y);
2596 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2597 ldisc_send(term->ldisc, buf, len, 0);
2598 }
2599 break;
2600 case 18:
2601 if (term->ldisc) {
2602 len = sprintf(buf, "\033[8;%d;%dt",
2603 term->rows, term->cols);
2604 ldisc_send(term->ldisc, buf, len, 0);
2605 }
2606 break;
2607 case 19:
2608 /*
2609 * Hmmm. Strictly speaking we
2610 * should return `the size of the
2611 * screen in characters', but
2612 * that's not easy: (a) window
2613 * furniture being what it is it's
2614 * hard to compute, and (b) in
2615 * resize-font mode maximising the
2616 * window wouldn't change the
2617 * number of characters. *shrug*. I
2618 * think we'll ignore it for the
2619 * moment and see if anyone
2620 * complains, and then ask them
2621 * what they would like it to do.
2622 */
2623 break;
2624 case 20:
2625 if (term->ldisc &&
2626 !term->cfg.no_remote_qtitle) {
2627 p = get_window_title(term->frontend, TRUE);
2628 len = strlen(p);
2629 ldisc_send(term->ldisc, "\033]L", 3, 0);
2630 ldisc_send(term->ldisc, p, len, 0);
2631 ldisc_send(term->ldisc, "\033\\", 2, 0);
2632 }
2633 break;
2634 case 21:
2635 if (term->ldisc &&
2636 !term->cfg.no_remote_qtitle) {
2637 p = get_window_title(term->frontend,FALSE);
2638 len = strlen(p);
2639 ldisc_send(term->ldisc, "\033]l", 3, 0);
2640 ldisc_send(term->ldisc, p, len, 0);
2641 ldisc_send(term->ldisc, "\033\\", 2, 0);
2642 }
2643 break;
2644 }
2645 }
2646 break;
2647 case 'S': /* SU: Scroll up */
2648 compatibility(SCOANSI);
2649 scroll(term, term->marg_t, term->marg_b,
2650 def(term->esc_args[0], 1), TRUE);
2651 fix_cpos;
2652 term->wrapnext = FALSE;
2653 term->seen_disp_event = TRUE;
2654 break;
2655 case 'T': /* SD: Scroll down */
2656 compatibility(SCOANSI);
2657 scroll(term, term->marg_t, term->marg_b,
2658 -def(term->esc_args[0], 1), TRUE);
2659 fix_cpos;
2660 term->wrapnext = FALSE;
2661 term->seen_disp_event = TRUE;
2662 break;
2663 case ANSI('|', '*'): /* DECSNLS */
2664 /*
2665 * Set number of lines on screen
2666 * VT420 uses VGA like hardware and can
2667 * support any size in reasonable range
2668 * (24..49 AIUI) with no default specified.
2669 */
2670 compatibility(VT420);
2671 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2672 if (!term->cfg.no_remote_resize)
2673 request_resize(term->frontend, term->cols,
2674 def(term->esc_args[0],
2675 term->cfg.height));
2676 deselect(term);
2677 }
2678 break;
2679 case ANSI('|', '$'): /* DECSCPP */
2680 /*
2681 * Set number of columns per page
2682 * Docs imply range is only 80 or 132, but
2683 * I'll allow any.
2684 */
2685 compatibility(VT340TEXT);
2686 if (term->esc_nargs <= 1) {
2687 if (!term->cfg.no_remote_resize)
2688 request_resize(term->frontend,
2689 def(term->esc_args[0],
2690 term->cfg.width), term->rows);
2691 deselect(term);
2692 }
2693 break;
2694 case 'X': /* ECH: write N spaces w/o moving cursor */
2695 /* XXX VTTEST says this is vt220, vt510 manual
2696 * says vt100 */
2697 compatibility(ANSIMIN);
2698 {
2699 int n = def(term->esc_args[0], 1);
2700 pos cursplus;
2701 unsigned long *p = term->cpos;
2702 if (n > term->cols - term->curs.x)
2703 n = term->cols - term->curs.x;
2704 cursplus = term->curs;
2705 cursplus.x += n;
2706 check_boundary(term, term->curs.x, term->curs.y);
2707 check_boundary(term, term->curs.x+n, term->curs.y);
2708 check_selection(term, term->curs, cursplus);
2709 while (n--)
2710 *p++ = term->erase_char;
2711 term->seen_disp_event = TRUE;
2712 }
2713 break;
2714 case 'x': /* DECREQTPARM: report terminal characteristics */
2715 compatibility(VT100);
2716 if (term->ldisc) {
2717 char buf[32];
2718 int i = def(term->esc_args[0], 0);
2719 if (i == 0 || i == 1) {
2720 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2721 buf[2] += i;
2722 ldisc_send(term->ldisc, buf, 20, 0);
2723 }
2724 }
2725 break;
2726 case 'Z': /* CBT: BackTab for xterm */
2727 compatibility(OTHER);
2728 {
2729 int i = def(term->esc_args[0], 1);
2730 pos old_curs = term->curs;
2731
2732 for(;i>0 && term->curs.x>0; i--) {
2733 do {
2734 term->curs.x--;
2735 } while (term->curs.x >0 &&
2736 !term->tabs[term->curs.x]);
2737 }
2738 fix_cpos;
2739 check_selection(term, old_curs, term->curs);
2740 }
2741 break;
2742 case ANSI('c', '='): /* Hide or Show Cursor */
2743 compatibility(SCOANSI);
2744 switch(term->esc_args[0]) {
2745 case 0: /* hide cursor */
2746 term->cursor_on = FALSE;
2747 break;
2748 case 1: /* restore cursor */
2749 term->big_cursor = FALSE;
2750 term->cursor_on = TRUE;
2751 break;
2752 case 2: /* block cursor */
2753 term->big_cursor = TRUE;
2754 term->cursor_on = TRUE;
2755 break;
2756 }
2757 break;
2758 case ANSI('C', '='):
2759 /*
2760 * set cursor start on scanline esc_args[0] and
2761 * end on scanline esc_args[1].If you set
2762 * the bottom scan line to a value less than
2763 * the top scan line, the cursor will disappear.
2764 */
2765 compatibility(SCOANSI);
2766 if (term->esc_nargs >= 2) {
2767 if (term->esc_args[0] > term->esc_args[1])
2768 term->cursor_on = FALSE;
2769 else
2770 term->cursor_on = TRUE;
2771 }
2772 break;
2773 case ANSI('D', '='):
2774 compatibility(SCOANSI);
2775 term->blink_is_real = FALSE;
2776 if (term->esc_args[0]>=1)
2777 term->curr_attr |= ATTR_BLINK;
2778 else
2779 term->curr_attr &= ~ATTR_BLINK;
2780 break;
2781 case ANSI('E', '='):
2782 compatibility(SCOANSI);
2783 term->blink_is_real = (term->esc_args[0] >= 1);
2784 break;
2785 case ANSI('F', '='): /* set normal foreground */
2786 compatibility(SCOANSI);
2787 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2788 long colour =
2789 (sco2ansicolour[term->esc_args[0] & 0x7] |
2790 ((term->esc_args[0] & 0x8) << 1)) <<
2791 ATTR_FGSHIFT;
2792 term->curr_attr &= ~ATTR_FGMASK;
2793 term->curr_attr |= colour;
2794 term->default_attr &= ~ATTR_FGMASK;
2795 term->default_attr |= colour;
2796 }
2797 break;
2798 case ANSI('G', '='): /* set normal background */
2799 compatibility(SCOANSI);
2800 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2801 long colour =
2802 (sco2ansicolour[term->esc_args[0] & 0x7] |
2803 ((term->esc_args[0] & 0x8) << 1)) <<
2804 ATTR_BGSHIFT;
2805 term->curr_attr &= ~ATTR_BGMASK;
2806 term->curr_attr |= colour;
2807 term->default_attr &= ~ATTR_BGMASK;
2808 term->default_attr |= colour;
2809 }
2810 break;
2811 case ANSI('L', '='):
2812 compatibility(SCOANSI);
2813 term->use_bce = (term->esc_args[0] <= 0);
2814 term->erase_char = ERASE_CHAR;
2815 if (term->use_bce)
2816 term->erase_char = (' ' | ATTR_ASCII |
2817 (term->curr_attr &
2818 (ATTR_FGMASK | ATTR_BGMASK)));
2819 break;
2820 case ANSI('p', '"'): /* DECSCL: set compat level */
2821 /*
2822 * Allow the host to make this emulator a
2823 * 'perfect' VT102. This first appeared in
2824 * the VT220, but we do need to get back to
2825 * PuTTY mode so I won't check it.
2826 *
2827 * The arg in 40..42,50 are a PuTTY extension.
2828 * The 2nd arg, 8bit vs 7bit is not checked.
2829 *
2830 * Setting VT102 mode should also change
2831 * the Fkeys to generate PF* codes as a
2832 * real VT102 has no Fkeys. The VT220 does
2833 * this, F11..F13 become ESC,BS,LF other
2834 * Fkeys send nothing.
2835 *
2836 * Note ESC c will NOT change this!
2837 */
2838
2839 switch (term->esc_args[0]) {
2840 case 61:
2841 term->compatibility_level &= ~TM_VTXXX;
2842 term->compatibility_level |= TM_VT102;
2843 break;
2844 case 62:
2845 term->compatibility_level &= ~TM_VTXXX;
2846 term->compatibility_level |= TM_VT220;
2847 break;
2848
2849 default:
2850 if (term->esc_args[0] > 60 &&
2851 term->esc_args[0] < 70)
2852 term->compatibility_level |= TM_VTXXX;
2853 break;
2854
2855 case 40:
2856 term->compatibility_level &= TM_VTXXX;
2857 break;
2858 case 41:
2859 term->compatibility_level = TM_PUTTY;
2860 break;
2861 case 42:
2862 term->compatibility_level = TM_SCOANSI;
2863 break;
2864
2865 case ARG_DEFAULT:
2866 term->compatibility_level = TM_PUTTY;
2867 break;
2868 case 50:
2869 break;
2870 }
2871
2872 /* Change the response to CSI c */
2873 if (term->esc_args[0] == 50) {
2874 int i;
2875 char lbuf[64];
2876 strcpy(term->id_string, "\033[?");
2877 for (i = 1; i < term->esc_nargs; i++) {
2878 if (i != 1)
2879 strcat(term->id_string, ";");
2880 sprintf(lbuf, "%d", term->esc_args[i]);
2881 strcat(term->id_string, lbuf);
2882 }
2883 strcat(term->id_string, "c");
2884 }
2885 #if 0
2886 /* Is this a good idea ?
2887 * Well we should do a soft reset at this point ...
2888 */
2889 if (!has_compat(VT420) && has_compat(VT100)) {
2890 if (!term->cfg.no_remote_resize) {
2891 if (term->reset_132)
2892 request_resize(132, 24);
2893 else
2894 request_resize(80, 24);
2895 }
2896 }
2897 #endif
2898 break;
2899 }
2900 break;
2901 case SEEN_OSC:
2902 term->osc_w = FALSE;
2903 switch (c) {
2904 case 'P': /* Linux palette sequence */
2905 term->termstate = SEEN_OSC_P;
2906 term->osc_strlen = 0;
2907 break;
2908 case 'R': /* Linux palette reset */
2909 palette_reset(term->frontend);
2910 term_invalidate(term);
2911 term->termstate = TOPLEVEL;
2912 break;
2913 case 'W': /* word-set */
2914 term->termstate = SEEN_OSC_W;
2915 term->osc_w = TRUE;
2916 break;
2917 case '0':
2918 case '1':
2919 case '2':
2920 case '3':
2921 case '4':
2922 case '5':
2923 case '6':
2924 case '7':
2925 case '8':
2926 case '9':
2927 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2928 break;
2929 case 'L':
2930 /*
2931 * Grotty hack to support xterm and DECterm title
2932 * sequences concurrently.
2933 */
2934 if (term->esc_args[0] == 2) {
2935 term->esc_args[0] = 1;
2936 break;
2937 }
2938 /* else fall through */
2939 default:
2940 term->termstate = OSC_STRING;
2941 term->osc_strlen = 0;
2942 }
2943 break;
2944 case OSC_STRING:
2945 /*
2946 * This OSC stuff is EVIL. It takes just one character to get into
2947 * sysline mode and it's not initially obvious how to get out.
2948 * So I've added CR and LF as string aborts.
2949 * This shouldn't effect compatibility as I believe embedded
2950 * control characters are supposed to be interpreted (maybe?)
2951 * and they don't display anything useful anyway.
2952 *
2953 * -- RDB
2954 */
2955 if (c == '\012' || c == '\015') {
2956 term->termstate = TOPLEVEL;
2957 } else if (c == 0234 || c == '\007') {
2958 /*
2959 * These characters terminate the string; ST and BEL
2960 * terminate the sequence and trigger instant
2961 * processing of it, whereas ESC goes back to SEEN_ESC
2962 * mode unless it is followed by \, in which case it is
2963 * synonymous with ST in the first place.
2964 */
2965 do_osc(term);
2966 term->termstate = TOPLEVEL;
2967 } else if (c == '\033')
2968 term->termstate = OSC_MAYBE_ST;
2969 else if (term->osc_strlen < OSC_STR_MAX)
2970 term->osc_string[term->osc_strlen++] = c;
2971 break;
2972 case SEEN_OSC_P:
2973 {
2974 int max = (term->osc_strlen == 0 ? 21 : 16);
2975 int val;
2976 if (c >= '0' && c <= '9')
2977 val = c - '0';
2978 else if (c >= 'A' && c <= 'A' + max - 10)
2979 val = c - 'A' + 10;
2980 else if (c >= 'a' && c <= 'a' + max - 10)
2981 val = c - 'a' + 10;
2982 else {
2983 term->termstate = TOPLEVEL;
2984 break;
2985 }
2986 term->osc_string[term->osc_strlen++] = val;
2987 if (term->osc_strlen >= 7) {
2988 palette_set(term->frontend, term->osc_string[0],
2989 term->osc_string[1] * 16 + term->osc_string[2],
2990 term->osc_string[3] * 16 + term->osc_string[4],
2991 term->osc_string[5] * 16 + term->osc_string[6]);
2992 term_invalidate(term);
2993 term->termstate = TOPLEVEL;
2994 }
2995 }
2996 break;
2997 case SEEN_OSC_W:
2998 switch (c) {
2999 case '0':
3000 case '1':
3001 case '2':
3002 case '3':
3003 case '4':
3004 case '5':
3005 case '6':
3006 case '7':
3007 case '8':
3008 case '9':
3009 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3010 break;
3011 default:
3012 term->termstate = OSC_STRING;
3013 term->osc_strlen = 0;
3014 }
3015 break;
3016 case VT52_ESC:
3017 term->termstate = TOPLEVEL;
3018 term->seen_disp_event = TRUE;
3019 switch (c) {
3020 case 'A':
3021 move(term, term->curs.x, term->curs.y - 1, 1);
3022 break;
3023 case 'B':
3024 move(term, term->curs.x, term->curs.y + 1, 1);
3025 break;
3026 case 'C':
3027 move(term, term->curs.x + 1, term->curs.y, 1);
3028 break;
3029 case 'D':
3030 move(term, term->curs.x - 1, term->curs.y, 1);
3031 break;
3032 /*
3033 * From the VT100 Manual
3034 * NOTE: The special graphics characters in the VT100
3035 * are different from those in the VT52
3036 *
3037 * From VT102 manual:
3038 * 137 _ Blank - Same
3039 * 140 ` Reserved - Humm.
3040 * 141 a Solid rectangle - Similar
3041 * 142 b 1/ - Top half of fraction for the
3042 * 143 c 3/ - subscript numbers below.
3043 * 144 d 5/
3044 * 145 e 7/
3045 * 146 f Degrees - Same
3046 * 147 g Plus or minus - Same
3047 * 150 h Right arrow
3048 * 151 i Ellipsis (dots)
3049 * 152 j Divide by
3050 * 153 k Down arrow
3051 * 154 l Bar at scan 0
3052 * 155 m Bar at scan 1
3053 * 156 n Bar at scan 2
3054 * 157 o Bar at scan 3 - Similar
3055 * 160 p Bar at scan 4 - Similar
3056 * 161 q Bar at scan 5 - Similar
3057 * 162 r Bar at scan 6 - Same
3058 * 163 s Bar at scan 7 - Similar
3059 * 164 t Subscript 0
3060 * 165 u Subscript 1
3061 * 166 v Subscript 2
3062 * 167 w Subscript 3
3063 * 170 x Subscript 4
3064 * 171 y Subscript 5
3065 * 172 z Subscript 6
3066 * 173 { Subscript 7
3067 * 174 | Subscript 8
3068 * 175 } Subscript 9
3069 * 176 ~ Paragraph
3070 *
3071 */
3072 case 'F':
3073 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
3074 break;
3075 case 'G':
3076 term->cset_attr[term->cset = 0] = ATTR_ASCII;
3077 break;
3078 case 'H':
3079 move(term, 0, 0, 0);
3080 break;
3081 case 'I':
3082 if (term->curs.y == 0)
3083 scroll(term, 0, term->rows - 1, -1, TRUE);
3084 else if (term->curs.y > 0)
3085 term->curs.y--;
3086 fix_cpos;
3087 term->wrapnext = FALSE;
3088 break;
3089 case 'J':
3090 erase_lots(term, FALSE, FALSE, TRUE);
3091 term->disptop = 0;
3092 break;
3093 case 'K':
3094 erase_lots(term, TRUE, FALSE, TRUE);
3095 break;
3096 #if 0
3097 case 'V':
3098 /* XXX Print cursor line */
3099 break;
3100 case 'W':
3101 /* XXX Start controller mode */
3102 break;
3103 case 'X':
3104 /* XXX Stop controller mode */
3105 break;
3106 #endif
3107 case 'Y':
3108 term->termstate = VT52_Y1;
3109 break;
3110 case 'Z':
3111 if (term->ldisc)
3112 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3113 break;
3114 case '=':
3115 term->app_keypad_keys = TRUE;
3116 break;
3117 case '>':
3118 term->app_keypad_keys = FALSE;
3119 break;
3120 case '<':
3121 /* XXX This should switch to VT100 mode not current or default
3122 * VT mode. But this will only have effect in a VT220+
3123 * emulation.
3124 */
3125 term->vt52_mode = FALSE;
3126 term->blink_is_real = term->cfg.blinktext;
3127 break;
3128 #if 0
3129 case '^':
3130 /* XXX Enter auto print mode */
3131 break;
3132 case '_':
3133 /* XXX Exit auto print mode */
3134 break;
3135 case ']':
3136 /* XXX Print screen */
3137 break;
3138 #endif
3139
3140 #ifdef VT52_PLUS
3141 case 'E':
3142 /* compatibility(ATARI) */
3143 move(term, 0, 0, 0);
3144 erase_lots(term, FALSE, FALSE, TRUE);
3145 term->disptop = 0;
3146 break;
3147 case 'L':
3148 /* compatibility(ATARI) */
3149 if (term->curs.y <= term->marg_b)
3150 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3151 break;
3152 case 'M':
3153 /* compatibility(ATARI) */
3154 if (term->curs.y <= term->marg_b)
3155 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3156 break;
3157 case 'b':
3158 /* compatibility(ATARI) */
3159 term->termstate = VT52_FG;
3160 break;
3161 case 'c':
3162 /* compatibility(ATARI) */
3163 term->termstate = VT52_BG;
3164 break;
3165 case 'd':
3166 /* compatibility(ATARI) */
3167 erase_lots(term, FALSE, TRUE, FALSE);
3168 term->disptop = 0;
3169 break;
3170 case 'e':
3171 /* compatibility(ATARI) */
3172 term->cursor_on = TRUE;
3173 break;
3174 case 'f':
3175 /* compatibility(ATARI) */
3176 term->cursor_on = FALSE;
3177 break;
3178 /* case 'j': Save cursor position - broken on ST */
3179 /* case 'k': Restore cursor position */
3180 case 'l':
3181 /* compatibility(ATARI) */
3182 erase_lots(term, TRUE, TRUE, TRUE);
3183 term->curs.x = 0;
3184 term->wrapnext = FALSE;
3185 fix_cpos;
3186 break;
3187 case 'o':
3188 /* compatibility(ATARI) */
3189 erase_lots(term, TRUE, TRUE, FALSE);
3190 break;
3191 case 'p':
3192 /* compatibility(ATARI) */
3193 term->curr_attr |= ATTR_REVERSE;
3194 break;
3195 case 'q':
3196 /* compatibility(ATARI) */
3197 term->curr_attr &= ~ATTR_REVERSE;
3198 break;
3199 case 'v': /* wrap Autowrap on - Wyse style */
3200 /* compatibility(ATARI) */
3201 term->wrap = 1;
3202 break;
3203 case 'w': /* Autowrap off */
3204 /* compatibility(ATARI) */
3205 term->wrap = 0;
3206 break;
3207
3208 case 'R':
3209 /* compatibility(OTHER) */
3210 term->vt52_bold = FALSE;
3211 term->curr_attr = ATTR_DEFAULT;
3212 if (term->use_bce)
3213 term->erase_char = (' ' | ATTR_ASCII |
3214 (term->curr_attr &
3215 (ATTR_FGMASK | ATTR_BGMASK)));
3216 break;
3217 case 'S':
3218 /* compatibility(VI50) */
3219 term->curr_attr |= ATTR_UNDER;
3220 break;
3221 case 'W':
3222 /* compatibility(VI50) */
3223 term->curr_attr &= ~ATTR_UNDER;
3224 break;
3225 case 'U':
3226 /* compatibility(VI50) */
3227 term->vt52_bold = TRUE;
3228 term->curr_attr |= ATTR_BOLD;
3229 break;
3230 case 'T':
3231 /* compatibility(VI50) */
3232 term->vt52_bold = FALSE;
3233 term->curr_attr &= ~ATTR_BOLD;
3234 break;
3235 #endif
3236 }
3237 break;
3238 case VT52_Y1:
3239 term->termstate = VT52_Y2;
3240 move(term, term->curs.x, c - ' ', 0);
3241 break;
3242 case VT52_Y2:
3243 term->termstate = TOPLEVEL;
3244 move(term, c - ' ', term->curs.y, 0);
3245 break;
3246
3247 #ifdef VT52_PLUS
3248 case VT52_FG:
3249 term->termstate = TOPLEVEL;
3250 term->curr_attr &= ~ATTR_FGMASK;
3251 term->curr_attr &= ~ATTR_BOLD;
3252 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3253 if ((c & 0x8) || term->vt52_bold)
3254 term->curr_attr |= ATTR_BOLD;
3255
3256 if (term->use_bce)
3257 term->erase_char = (' ' | ATTR_ASCII |
3258 (term->curr_attr &
3259 (ATTR_FGMASK | ATTR_BGMASK)));
3260 break;
3261 case VT52_BG:
3262 term->termstate = TOPLEVEL;
3263 term->curr_attr &= ~ATTR_BGMASK;
3264 term->curr_attr &= ~ATTR_BLINK;
3265 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3266
3267 /* Note: bold background */
3268 if (c & 0x8)
3269 term->curr_attr |= ATTR_BLINK;
3270
3271 if (term->use_bce)
3272 term->erase_char = (' ' | ATTR_ASCII |
3273 (term->curr_attr &
3274 (ATTR_FGMASK | ATTR_BGMASK)));
3275 break;
3276 #endif
3277 default: break; /* placate gcc warning about enum use */
3278 }
3279 if (term->selstate != NO_SELECTION) {
3280 pos cursplus = term->curs;
3281 incpos(cursplus);
3282 check_selection(term, term->curs, cursplus);
3283 }
3284 }
3285
3286 term_print_flush(term);
3287 }
3288
3289 #if 0
3290 /*
3291 * Compare two lines to determine whether they are sufficiently
3292 * alike to scroll-optimise one to the other. Return the degree of
3293 * similarity.
3294 */
3295 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3296 {
3297 int i, n;
3298
3299 for (i = n = 0; i < term->cols; i++)
3300 n += (*a++ == *b++);
3301 return n;
3302 }
3303 #endif
3304
3305 /*
3306 * Given a context, update the window. Out of paranoia, we don't
3307 * allow WM_PAINT responses to do scrolling optimisations.
3308 */
3309 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3310 {
3311 int i, j, our_curs_y, our_curs_x;
3312 unsigned long rv, cursor;
3313 pos scrpos;
3314 char ch[1024];
3315 long cursor_background = ERASE_CHAR;
3316 unsigned long ticks;
3317 #ifdef OPTIMISE_SCROLL
3318 struct scrollregion *sr;
3319 #endif /* OPTIMISE_SCROLL */
3320
3321 /*
3322 * Check the visual bell state.
3323 */
3324 if (term->in_vbell) {
3325 ticks = GETTICKCOUNT();
3326 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3327 term->in_vbell = FALSE;
3328 }
3329
3330 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3331
3332 /* Depends on:
3333 * screen array, disptop, scrtop,
3334 * selection, rv,
3335 * cfg.blinkpc, blink_is_real, tblinker,
3336 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3337 */
3338
3339 /* Has the cursor position or type changed ? */
3340 if (term->cursor_on) {
3341 if (term->has_focus) {
3342 if (term->blinker || !term->cfg.blink_cur)
3343 cursor = TATTR_ACTCURS;
3344 else
3345 cursor = 0;
3346 } else
3347 cursor = TATTR_PASCURS;
3348 if (term->wrapnext)
3349 cursor |= TATTR_RIGHTCURS;
3350 } else
3351 cursor = 0;
3352 our_curs_y = term->curs.y - term->disptop;
3353 {
3354 /*
3355 * Adjust the cursor position in the case where it's
3356 * resting on the right-hand half of a CJK wide character.
3357 * xterm's behaviour here, which seems adequate to me, is
3358 * to display the cursor covering the _whole_ character,
3359 * exactly as if it were one space to the left.
3360 */
3361 unsigned long *ldata = lineptr(term->curs.y);
3362 our_curs_x = term->curs.x;
3363 if (our_curs_x > 0 &&
3364 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3365 our_curs_x--;
3366 }
3367
3368 if (term->dispcurs && (term->curstype != cursor ||
3369 term->dispcurs !=
3370 term->disptext + our_curs_y * (term->cols + 1) +
3371 our_curs_x)) {
3372 if (term->dispcurs > term->disptext &&
3373 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3374 term->dispcurs[-1] |= ATTR_INVALID;
3375 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3376 term->dispcurs[1] |= ATTR_INVALID;
3377 *term->dispcurs |= ATTR_INVALID;
3378 term->curstype = 0;
3379 }
3380 term->dispcurs = NULL;
3381
3382 #ifdef OPTIMISE_SCROLL
3383 /* Do scrolls */
3384 sr = term->scrollhead;
3385 while (sr) {
3386 struct scrollregion *next = sr->next;
3387 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3388 sfree(sr);
3389 sr = next;
3390 }
3391 term->scrollhead = term->scrolltail = NULL;
3392 #endif /* OPTIMISE_SCROLL */
3393
3394 /* The normal screen data */
3395 for (i = 0; i < term->rows; i++) {
3396 unsigned long *ldata;
3397 int lattr;
3398 int idx, dirty_line, dirty_run, selected;
3399 unsigned long attr = 0;
3400 int updated_line = 0;
3401 int start = 0;
3402 int ccount = 0;
3403 int last_run_dirty = 0;
3404
3405 scrpos.y = i + term->disptop;
3406 ldata = lineptr(scrpos.y);
3407 lattr = (ldata[term->cols] & LATTR_MODE);
3408
3409 idx = i * (term->cols + 1);
3410 dirty_run = dirty_line = (ldata[term->cols] !=
3411 term->disptext[idx + term->cols]);
3412 term->disptext[idx + term->cols] = ldata[term->cols];
3413
3414 for (j = 0; j < term->cols; j++, idx++) {
3415 unsigned long tattr, tchar;
3416 unsigned long *d = ldata + j;
3417 int break_run;
3418 scrpos.x = j;
3419
3420 tchar = (*d & (CHAR_MASK | CSET_MASK));
3421 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3422 switch (tchar & CSET_MASK) {
3423 case ATTR_ASCII:
3424 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3425 break;
3426 case ATTR_LINEDRW:
3427 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3428 break;
3429 case ATTR_SCOACS:
3430 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3431 break;
3432 }
3433 tattr |= (tchar & CSET_MASK);
3434 tchar &= CHAR_MASK;
3435 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3436 tattr |= ATTR_WIDE;
3437
3438 /* Video reversing things */
3439 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3440 if (term->seltype == LEXICOGRAPHIC)
3441 selected = (posle(term->selstart, scrpos) &&
3442 poslt(scrpos, term->selend));
3443 else
3444 selected = (posPle(term->selstart, scrpos) &&
3445 posPlt(scrpos, term->selend));
3446 } else
3447 selected = FALSE;
3448 tattr = (tattr ^ rv
3449 ^ (selected ? ATTR_REVERSE : 0));
3450
3451 /* 'Real' blinking ? */
3452 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3453 if (term->has_focus && term->tblinker) {
3454 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
3455 }
3456 tattr &= ~ATTR_BLINK;
3457 }
3458
3459 /*
3460 * Check the font we'll _probably_ be using to see if
3461 * the character is wide when we don't want it to be.
3462 */
3463 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3464 if ((tattr & ATTR_WIDE) == 0 &&
3465 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3466 tattr |= ATTR_NARROW;
3467 } else if (term->disptext[idx]&ATTR_NARROW)
3468 tattr |= ATTR_NARROW;
3469
3470 /* Cursor here ? Save the 'background' */
3471 if (i == our_curs_y && j == our_curs_x) {
3472 cursor_background = tattr | tchar;
3473 term->dispcurs = term->disptext + idx;
3474 }
3475
3476 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3477 dirty_line = TRUE;
3478
3479 break_run = (((tattr ^ attr) & term->attr_mask) ||
3480 j - start >= sizeof(ch));
3481
3482 /* Special hack for VT100 Linedraw glyphs */
3483 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3484 && tchar <= 0xBD) break_run = TRUE;
3485
3486 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3487 if ((tchar | tattr) == term->disptext[idx])
3488 break_run = TRUE;
3489 else if (!dirty_run && ccount == 1)
3490 break_run = TRUE;
3491 }
3492
3493 if (break_run) {
3494 if ((dirty_run || last_run_dirty) && ccount > 0) {
3495 do_text(ctx, start, i, ch, ccount, attr, lattr);
3496 updated_line = 1;
3497 }
3498 start = j;
3499 ccount = 0;
3500 attr = tattr;
3501 if (term->ucsdata->dbcs_screenfont)
3502 last_run_dirty = dirty_run;
3503 dirty_run = dirty_line;
3504 }
3505
3506 if ((tchar | tattr) != term->disptext[idx])
3507 dirty_run = TRUE;
3508 ch[ccount++] = (char) tchar;
3509 term->disptext[idx] = tchar | tattr;
3510
3511 /* If it's a wide char step along to the next one. */
3512 if (tattr & ATTR_WIDE) {
3513 if (++j < term->cols) {
3514 idx++;
3515 d++;
3516 /*
3517 * By construction above, the cursor should not
3518 * be on the right-hand half of this character.
3519 * Ever.
3520 */
3521 assert(!(i == our_curs_y && j == our_curs_x));
3522 if (term->disptext[idx] != *d)
3523 dirty_run = TRUE;
3524 term->disptext[idx] = *d;
3525 }
3526 }
3527 }
3528 if (dirty_run && ccount > 0) {
3529 do_text(ctx, start, i, ch, ccount, attr, lattr);
3530 updated_line = 1;
3531 }
3532
3533 /* Cursor on this line ? (and changed) */
3534 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3535 ch[0] = (char) (cursor_background & CHAR_MASK);
3536 attr = (cursor_background & ATTR_MASK) | cursor;
3537 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3538 term->curstype = cursor;
3539 }
3540 }
3541 }
3542
3543 /*
3544 * Flick the switch that says if blinking things should be shown or hidden.
3545 */
3546
3547 void term_blink(Terminal *term, int flg)
3548 {
3549 long now, blink_diff;
3550
3551 now = GETTICKCOUNT();
3552 blink_diff = now - term->last_tblink;
3553
3554 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3555 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3556 term->last_tblink = now;
3557 term->tblinker = !term->tblinker;
3558 }
3559
3560 if (flg) {
3561 term->blinker = 1;
3562 term->last_blink = now;
3563 return;
3564 }
3565
3566 blink_diff = now - term->last_blink;
3567
3568 /* Make sure the cursor blinks no faster than system blink rate */
3569 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3570 return;
3571
3572 term->last_blink = now;
3573 term->blinker = !term->blinker;
3574 }
3575
3576 /*
3577 * Invalidate the whole screen so it will be repainted in full.
3578 */
3579 void term_invalidate(Terminal *term)
3580 {
3581 int i;
3582
3583 for (i = 0; i < term->rows * (term->cols + 1); i++)
3584 term->disptext[i] = ATTR_INVALID;
3585 }
3586
3587 /*
3588 * Paint the window in response to a WM_PAINT message.
3589 */
3590 void term_paint(Terminal *term, Context ctx,
3591 int left, int top, int right, int bottom, int immediately)
3592 {
3593 int i, j;
3594 if (left < 0) left = 0;
3595 if (top < 0) top = 0;
3596 if (right >= term->cols) right = term->cols-1;
3597 if (bottom >= term->rows) bottom = term->rows-1;
3598
3599 for (i = top; i <= bottom && i < term->rows; i++) {
3600 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3601 LATTR_MODE) == LATTR_NORM)
3602 for (j = left; j <= right && j < term->cols; j++)
3603 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3604 else
3605 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3606 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3607 }
3608
3609 /* This should happen soon enough, also for some reason it sometimes
3610 * fails to actually do anything when re-sizing ... painting the wrong
3611 * window perhaps ?
3612 */
3613 if (immediately)
3614 do_paint (term, ctx, FALSE);
3615 }
3616
3617 /*
3618 * Attempt to scroll the scrollback. The second parameter gives the
3619 * position we want to scroll to; the first is +1 to denote that
3620 * this position is relative to the beginning of the scrollback, -1
3621 * to denote it is relative to the end, and 0 to denote that it is
3622 * relative to the current position.
3623 */
3624 void term_scroll(Terminal *term, int rel, int where)
3625 {
3626 int sbtop = -sblines(term);
3627 #ifdef OPTIMISE_SCROLL
3628 int olddisptop = term->disptop;
3629 int shift;
3630 #endif /* OPTIMISE_SCROLL */
3631
3632 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3633 if (term->disptop < sbtop)
3634 term->disptop = sbtop;
3635 if (term->disptop > 0)
3636 term->disptop = 0;
3637 update_sbar(term);
3638 #ifdef OPTIMISE_SCROLL
3639 shift = (term->disptop - olddisptop);
3640 if (shift < term->rows && shift > -term->rows)
3641 scroll_display(term, 0, term->rows - 1, shift);
3642 #endif /* OPTIMISE_SCROLL */
3643 term_update(term);
3644 }
3645
3646 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
3647 {
3648 wchar_t *workbuf;
3649 wchar_t *wbptr; /* where next char goes within workbuf */
3650 int old_top_x;
3651 int wblen = 0; /* workbuf len */
3652 int buflen; /* amount of memory allocated to workbuf */
3653
3654 buflen = 5120; /* Default size */
3655 workbuf = snewn(buflen, wchar_t);
3656 wbptr = workbuf; /* start filling here */
3657 old_top_x = top.x; /* needed for rect==1 */
3658
3659 while (poslt(top, bottom)) {
3660 int nl = FALSE;
3661 unsigned long *ldata = lineptr(top.y);
3662 pos nlpos;
3663
3664 /*
3665 * nlpos will point at the maximum position on this line we
3666 * should copy up to. So we start it at the end of the
3667 * line...
3668 */
3669 nlpos.y = top.y;
3670 nlpos.x = term->cols;
3671
3672 /*
3673 * ... move it backwards if there's unused space at the end
3674 * of the line (and also set `nl' if this is the case,
3675 * because in normal selection mode this means we need a
3676 * newline at the end)...
3677 */
3678 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3679 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3680 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3681 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3682 && poslt(top, nlpos))
3683 decpos(nlpos);
3684 if (poslt(nlpos, bottom))
3685 nl = TRUE;
3686 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3687 /* Ignore the last char on the line in a WRAPPED2 line. */
3688 decpos(nlpos);
3689 }
3690
3691 /*
3692 * ... and then clip it to the terminal x coordinate if
3693 * we're doing rectangular selection. (In this case we
3694 * still did the above, so that copying e.g. the right-hand
3695 * column from a table doesn't fill with spaces on the
3696 * right.)
3697 */
3698 if (rect) {
3699 if (nlpos.x > bottom.x)
3700 nlpos.x = bottom.x;
3701 nl = (top.y < bottom.y);
3702 }
3703
3704 while (poslt(top, bottom) && poslt(top, nlpos)) {
3705 #if 0
3706 char cbuf[16], *p;
3707 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3708 #else
3709 wchar_t cbuf[16], *p;
3710 int uc = (ldata[top.x] & 0xFFFF);
3711 int set, c;
3712
3713 if (uc == UCSWIDE) {
3714 top.x++;
3715 continue;
3716 }
3717
3718 switch (uc & CSET_MASK) {
3719 case ATTR_LINEDRW:
3720 if (!term->cfg.rawcnp) {
3721 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3722 break;
3723 }
3724 case ATTR_ASCII:
3725 uc = term->ucsdata->unitab_line[uc & 0xFF];
3726 break;
3727 case ATTR_SCOACS:
3728 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3729 break;
3730 }
3731 switch (uc & CSET_MASK) {
3732 case ATTR_ACP:
3733 uc = term->ucsdata->unitab_font[uc & 0xFF];
3734 break;
3735 case ATTR_OEMCP:
3736 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3737 break;
3738 }
3739
3740 set = (uc & CSET_MASK);
3741 c = (uc & CHAR_MASK);
3742 cbuf[0] = uc;
3743 cbuf[1] = 0;
3744
3745 if (DIRECT_FONT(uc)) {
3746 if (c >= ' ' && c != 0x7F) {
3747 char buf[4];
3748 WCHAR wbuf[4];
3749 int rv;
3750 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3751 buf[0] = c;
3752 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3753 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3754 top.x++;
3755 } else {
3756 buf[0] = c;
3757 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3758 }
3759
3760 if (rv > 0) {
3761 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3762 cbuf[rv] = 0;
3763 }
3764 }
3765 }
3766 #endif
3767
3768 for (p = cbuf; *p; p++) {
3769 /* Enough overhead for trailing NL and nul */
3770 if (wblen >= buflen - 16) {
3771 buflen += 100;
3772 workbuf = sresize(workbuf, buflen, wchar_t);
3773 wbptr = workbuf + wblen;
3774 }
3775 wblen++;
3776 *wbptr++ = *p;
3777 }
3778 top.x++;
3779 }
3780 if (nl) {
3781 int i;
3782 for (i = 0; i < sel_nl_sz; i++) {
3783 wblen++;
3784 *wbptr++ = sel_nl[i];
3785 }
3786 }
3787 top.y++;
3788 top.x = rect ? old_top_x : 0;
3789 }
3790 #if SELECTION_NUL_TERMINATED
3791 wblen++;
3792 *wbptr++ = 0;
3793 #endif
3794 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
3795 if (buflen > 0) /* indicates we allocated this buffer */
3796 sfree(workbuf);
3797 }
3798
3799 void term_copyall(Terminal *term)
3800 {
3801 pos top;
3802 pos bottom;
3803 tree234 *screen = term->screen;
3804 top.y = -sblines(term);
3805 top.x = 0;
3806 bottom.y = find_last_nonempty_line(term, screen);
3807 bottom.x = term->cols;
3808 clipme(term, top, bottom, 0, TRUE);
3809 }
3810
3811 /*
3812 * The wordness array is mainly for deciding the disposition of the
3813 * US-ASCII characters.
3814 */
3815 static int wordtype(Terminal *term, int uc)
3816 {
3817 struct ucsword {
3818 int start, end, ctype;
3819 };
3820 static const struct ucsword ucs_words[] = {
3821 {
3822 128, 160, 0}, {
3823 161, 191, 1}, {
3824 215, 215, 1}, {
3825 247, 247, 1}, {
3826 0x037e, 0x037e, 1}, /* Greek question mark */
3827 {
3828 0x0387, 0x0387, 1}, /* Greek ano teleia */
3829 {
3830 0x055a, 0x055f, 1}, /* Armenian punctuation */
3831 {
3832 0x0589, 0x0589, 1}, /* Armenian full stop */
3833 {
3834 0x0700, 0x070d, 1}, /* Syriac punctuation */
3835 {
3836 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3837 {
3838 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3839 {
3840 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3841 {
3842 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3843 {
3844 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3845 {
3846 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3847 {
3848 0x2000, 0x200a, 0}, /* Various spaces */
3849 {
3850 0x2070, 0x207f, 2}, /* superscript */
3851 {
3852 0x2080, 0x208f, 2}, /* subscript */
3853 {
3854 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3855 {
3856 0x3000, 0x3000, 0}, /* ideographic space */
3857 {
3858 0x3001, 0x3020, 1}, /* ideographic punctuation */
3859 {
3860 0x303f, 0x309f, 3}, /* Hiragana */
3861 {
3862 0x30a0, 0x30ff, 3}, /* Katakana */
3863 {
3864 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3865 {
3866 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3867 {
3868 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3869 {
3870 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3871 {
3872 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3873 {
3874 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3875 {
3876 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3877 {
3878 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3879 {
3880 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3881 {
3882 0, 0, 0}
3883 };
3884 const struct ucsword *wptr;
3885
3886 uc &= (CSET_MASK | CHAR_MASK);
3887
3888 switch (uc & CSET_MASK) {
3889 case ATTR_LINEDRW:
3890 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3891 break;
3892 case ATTR_ASCII:
3893 uc = term->ucsdata->unitab_line[uc & 0xFF];
3894 break;
3895 case ATTR_SCOACS:
3896 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3897 break;
3898 }
3899 switch (uc & CSET_MASK) {
3900 case ATTR_ACP:
3901 uc = term->ucsdata->unitab_font[uc & 0xFF];
3902 break;
3903 case ATTR_OEMCP:
3904 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3905 break;
3906 }
3907
3908 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3909 * fail as there's such a thing as a double width space. :-(
3910 */
3911 if (term->ucsdata->dbcs_screenfont &&
3912 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3913 return (uc != ' ');
3914
3915 if (uc < 0x80)
3916 return term->wordness[uc];
3917
3918 for (wptr = ucs_words; wptr->start; wptr++) {
3919 if (uc >= wptr->start && uc <= wptr->end)
3920 return wptr->ctype;
3921 }
3922
3923 return 2;
3924 }
3925
3926 /*
3927 * Spread the selection outwards according to the selection mode.
3928 */
3929 static pos sel_spread_half(Terminal *term, pos p, int dir)
3930 {
3931 unsigned long *ldata;
3932 short wvalue;
3933 int topy = -sblines(term);
3934
3935 ldata = lineptr(p.y);
3936
3937 switch (term->selmode) {
3938 case SM_CHAR:
3939 /*
3940 * In this mode, every character is a separate unit, except
3941 * for runs of spaces at the end of a non-wrapping line.
3942 */
3943 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3944 unsigned long *q = ldata + term->cols;
3945 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3946 q--;
3947 if (q == ldata + term->cols)
3948 q--;
3949 if (p.x >= q - ldata)
3950 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3951 }
3952 break;
3953 case SM_WORD:
3954 /*
3955 * In this mode, the units are maximal runs of characters
3956 * whose `wordness' has the same value.
3957 */
3958 wvalue = wordtype(term, UCSGET(ldata, p.x));
3959 if (dir == +1) {
3960 while (1) {
3961 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3962 term->cols-1 : term->cols);
3963 if (p.x < maxcols-1) {
3964 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3965 p.x++;
3966 else
3967 break;
3968 } else {
3969 if (ldata[term->cols] & LATTR_WRAPPED) {
3970 unsigned long *ldata2;
3971 ldata2 = lineptr(p.y+1);
3972 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3973 p.x = 0;
3974 p.y++;
3975 ldata = ldata2;
3976 } else
3977 break;
3978 } else
3979 break;
3980 }
3981 }
3982 } else {
3983 while (1) {
3984 if (p.x > 0) {
3985 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3986 p.x--;
3987 else
3988 break;
3989 } else {
3990 unsigned long *ldata2;
3991 int maxcols;
3992 if (p.y <= topy)
3993 break;
3994 ldata2 = lineptr(p.y-1);
3995 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3996 term->cols-1 : term->cols);
3997 if (ldata2[term->cols] & LATTR_WRAPPED) {
3998 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3999 == wvalue) {
4000 p.x = maxcols-1;
4001 p.y--;
4002 ldata = ldata2;
4003 } else
4004 break;
4005 } else
4006 break;
4007 }
4008 }
4009 }
4010 break;
4011 case SM_LINE:
4012 /*
4013 * In this mode, every line is a unit.
4014 */
4015 p.x = (dir == -1 ? 0 : term->cols - 1);
4016 break;
4017 }
4018 return p;
4019 }
4020
4021 static void sel_spread(Terminal *term)
4022 {
4023 if (term->seltype == LEXICOGRAPHIC) {
4024 term->selstart = sel_spread_half(term, term->selstart, -1);
4025 decpos(term->selend);
4026 term->selend = sel_spread_half(term, term->selend, +1);
4027 incpos(term->selend);
4028 }
4029 }
4030
4031 void term_do_paste(Terminal *term)
4032 {
4033 wchar_t *data;
4034 int len;
4035
4036 get_clip(term->frontend, &data, &len);
4037 if (data && len > 0) {
4038 wchar_t *p, *q;
4039
4040 term_seen_key_event(term); /* pasted data counts */
4041
4042 if (term->paste_buffer)
4043 sfree(term->paste_buffer);
4044 term->paste_pos = term->paste_hold = term->paste_len = 0;
4045 term->paste_buffer = snewn(len, wchar_t);
4046
4047 p = q = data;
4048 while (p < data + len) {
4049 while (p < data + len &&
4050 !(p <= data + len - sel_nl_sz &&
4051 !memcmp(p, sel_nl, sizeof(sel_nl))))
4052 p++;
4053
4054 {
4055 int i;
4056 for (i = 0; i < p - q; i++) {
4057 term->paste_buffer[term->paste_len++] = q[i];
4058 }
4059 }
4060
4061 if (p <= data + len - sel_nl_sz &&
4062 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4063 term->paste_buffer[term->paste_len++] = '\015';
4064 p += sel_nl_sz;
4065 }
4066 q = p;
4067 }
4068
4069 /* Assume a small paste will be OK in one go. */
4070 if (term->paste_len < 256) {
4071 if (term->ldisc)
4072 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4073 if (term->paste_buffer)
4074 sfree(term->paste_buffer);
4075 term->paste_buffer = 0;
4076 term->paste_pos = term->paste_hold = term->paste_len = 0;
4077 }
4078 }
4079 get_clip(term->frontend, NULL, NULL);
4080 }
4081
4082 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4083 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4084 {
4085 pos selpoint;
4086 unsigned long *ldata;
4087 int raw_mouse = (term->xterm_mouse &&
4088 !term->cfg.no_mouse_rep &&
4089 !(term->cfg.mouse_override && shift));
4090 int default_seltype;
4091
4092 if (y < 0) {
4093 y = 0;
4094 if (a == MA_DRAG && !raw_mouse)
4095 term_scroll(term, 0, -1);
4096 }
4097 if (y >= term->rows) {
4098 y = term->rows - 1;
4099 if (a == MA_DRAG && !raw_mouse)
4100 term_scroll(term, 0, +1);
4101 }
4102 if (x < 0) {
4103 if (y > 0) {
4104 x = term->cols - 1;
4105 y--;
4106 } else
4107 x = 0;
4108 }
4109 if (x >= term->cols)
4110 x = term->cols - 1;
4111
4112 selpoint.y = y + term->disptop;
4113 selpoint.x = x;
4114 ldata = lineptr(selpoint.y);
4115 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
4116 selpoint.x /= 2;
4117
4118 if (raw_mouse) {
4119 int encstate = 0, r, c;
4120 char abuf[16];
4121
4122 if (term->ldisc) {
4123
4124 switch (braw) {
4125 case MBT_LEFT:
4126 encstate = 0x20; /* left button down */
4127 break;
4128 case MBT_MIDDLE:
4129 encstate = 0x21;
4130 break;
4131 case MBT_RIGHT:
4132 encstate = 0x22;
4133 break;
4134 case MBT_WHEEL_UP:
4135 encstate = 0x60;
4136 break;
4137 case MBT_WHEEL_DOWN:
4138 encstate = 0x61;
4139 break;
4140 default: break; /* placate gcc warning about enum use */
4141 }
4142 switch (a) {
4143 case MA_DRAG:
4144 if (term->xterm_mouse == 1)
4145 return;
4146 encstate += 0x20;
4147 break;
4148 case MA_RELEASE:
4149 encstate = 0x23;
4150 term->mouse_is_down = 0;
4151 break;
4152 case MA_CLICK:
4153 if (term->mouse_is_down == braw)
4154 return;
4155 term->mouse_is_down = braw;
4156 break;
4157 default: break; /* placate gcc warning about enum use */
4158 }
4159 if (shift)
4160 encstate += 0x04;
4161 if (ctrl)
4162 encstate += 0x10;
4163 r = y + 33;
4164 c = x + 33;
4165
4166 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4167 ldisc_send(term->ldisc, abuf, 6, 0);
4168 }
4169 return;
4170 }
4171
4172 /*
4173 * Set the selection type (rectangular or normal) at the start
4174 * of a selection attempt, from the state of Alt.
4175 */
4176 if (!alt ^ !term->cfg.rect_select)
4177 default_seltype = RECTANGULAR;
4178 else
4179 default_seltype = LEXICOGRAPHIC;
4180
4181 if (term->selstate == NO_SELECTION) {
4182 term->seltype = default_seltype;
4183 }
4184
4185 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4186 deselect(term);
4187 term->selstate = ABOUT_TO;
4188 term->seltype = default_seltype;
4189 term->selanchor = selpoint;
4190 term->selmode = SM_CHAR;
4191 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4192 deselect(term);
4193 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4194 term->selstate = DRAGGING;
4195 term->selstart = term->selanchor = selpoint;
4196 term->selend = term->selstart;
4197 incpos(term->selend);
4198 sel_spread(term);
4199 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4200 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4201 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4202 return;
4203 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4204 term->selstate == SELECTED) {
4205 if (term->seltype == LEXICOGRAPHIC) {
4206 /*
4207 * For normal selection, we extend by moving
4208 * whichever end of the current selection is closer
4209 * to the mouse.
4210 */
4211 if (posdiff(selpoint, term->selstart) <
4212 posdiff(term->selend, term->selstart) / 2) {
4213 term->selanchor = term->selend;
4214 decpos(term->selanchor);
4215 } else {
4216 term->selanchor = term->selstart;
4217 }
4218 } else {
4219 /*
4220 * For rectangular selection, we have a choice of
4221 * _four_ places to put selanchor and selpoint: the
4222 * four corners of the selection.
4223 */
4224 if (2*selpoint.x < term->selstart.x + term->selend.x)
4225 term->selanchor.x = term->selend.x-1;
4226 else
4227 term->selanchor.x = term->selstart.x;
4228
4229 if (2*selpoint.y < term->selstart.y + term->selend.y)
4230 term->selanchor.y = term->selend.y;
4231 else
4232 term->selanchor.y = term->selstart.y;
4233 }
4234 term->selstate = DRAGGING;
4235 }
4236 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4237 term->selanchor = selpoint;
4238 term->selstate = DRAGGING;
4239 if (term->seltype == LEXICOGRAPHIC) {
4240 /*
4241 * For normal selection, we set (selstart,selend) to
4242 * (selpoint,selanchor) in some order.
4243 */
4244 if (poslt(selpoint, term->selanchor)) {
4245 term->selstart = selpoint;
4246 term->selend = term->selanchor;
4247 incpos(term->selend);
4248 } else {
4249 term->selstart = term->selanchor;
4250 term->selend = selpoint;
4251 incpos(term->selend);
4252 }
4253 } else {
4254 /*
4255 * For rectangular selection, we may need to
4256 * interchange x and y coordinates (if the user has
4257 * dragged in the -x and +y directions, or vice versa).
4258 */
4259 term->selstart.x = min(term->selanchor.x, selpoint.x);
4260 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4261 term->selstart.y = min(term->selanchor.y, selpoint.y);
4262 term->selend.y = max(term->selanchor.y, selpoint.y);
4263 }
4264 sel_spread(term);
4265 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4266 a == MA_RELEASE) {
4267 if (term->selstate == DRAGGING) {
4268 /*
4269 * We've completed a selection. We now transfer the
4270 * data to the clipboard.
4271 */
4272 clipme(term, term->selstart, term->selend,
4273 (term->seltype == RECTANGULAR), FALSE);
4274 term->selstate = SELECTED;
4275 } else
4276 term->selstate = NO_SELECTION;
4277 } else if (bcooked == MBT_PASTE
4278 && (a == MA_CLICK
4279 #if MULTICLICK_ONLY_EVENT
4280 || a == MA_2CLK || a == MA_3CLK
4281 #endif
4282 )) {
4283 request_paste(term->frontend);
4284 }
4285
4286 term_update(term);
4287 }
4288
4289 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4290 unsigned int modifiers, unsigned int flags)
4291 {
4292 char output[10];
4293 char *p = output;
4294 int prependesc = FALSE;
4295 #if 0
4296 int i;
4297
4298 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4299 for (i = 0; i < tlen; i++)
4300 fprintf(stderr, " %04x", (unsigned)text[i]);
4301 fprintf(stderr, "\n");
4302 #endif
4303
4304 /* XXX Num Lock */
4305 if ((flags & PKF_REPEAT) && term->repeat_off)
4306 return;
4307
4308 /* Currently, Meta always just prefixes everything with ESC. */
4309 if (modifiers & PKM_META)
4310 prependesc = TRUE;
4311 modifiers &= ~PKM_META;
4312
4313 /*
4314 * Alt is only used for Alt+keypad, which isn't supported yet, so
4315 * ignore it.
4316 */
4317 modifiers &= ~PKM_ALT;
4318
4319 /* Standard local function keys */
4320 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4321 case PKM_SHIFT:
4322 if (keysym == PK_PAGEUP)
4323 /* scroll up one page */;
4324 if (keysym == PK_PAGEDOWN)
4325 /* scroll down on page */;
4326 if (keysym == PK_INSERT)
4327 term_do_paste(term);
4328 break;
4329 case PKM_CONTROL:
4330 if (keysym == PK_PAGEUP)
4331 /* scroll up one line */;
4332 if (keysym == PK_PAGEDOWN)
4333 /* scroll down one line */;
4334 /* Control-Numlock for app-keypad mode switch */
4335 if (keysym == PK_PF1)
4336 term->app_keypad_keys ^= 1;
4337 break;
4338 }
4339
4340 if (modifiers & PKM_ALT) {
4341 /* Alt+F4 (close) */
4342 /* Alt+Return (full screen) */
4343 /* Alt+Space (system menu) */
4344 }
4345
4346 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4347 text[0] >= 0x20 && text[0] <= 0x7e) {
4348 /* ASCII chars + Control */
4349 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4350 (text[0] >= 0x61 && text[0] <= 0x7a))
4351 text[0] &= 0x1f;
4352 else {
4353 /*
4354 * Control-2 should return ^@ (0x00), Control-6 should return
4355 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4356 * the DOS keyboard handling did it, and we have nothing better
4357 * to do with the key combo in question, we'll also map
4358 * Control-Backquote to ^\ (0x1C).
4359 */
4360 switch (text[0]) {
4361 case ' ': text[0] = 0x00; break;
4362 case '-': text[0] = 0x1f; break;
4363 case '/': text[0] = 0x1f; break;
4364 case '2': text[0] = 0x00; break;
4365 case '3': text[0] = 0x1b; break;
4366 case '4': text[0] = 0x1c; break;
4367 case '5': text[0] = 0x1d; break;
4368 case '6': text[0] = 0x1e; break;
4369 case '7': text[0] = 0x1f; break;
4370 case '8': text[0] = 0x7f; break;
4371 case '`': text[0] = 0x1c; break;
4372 }
4373 }
4374 }
4375
4376 /* Nethack keypad */
4377 if (term->cfg.nethack_keypad) {
4378 char c = 0;
4379 switch (keysym) {
4380 case PK_KP1: c = 'b'; break;
4381 case PK_KP2: c = 'j'; break;
4382 case PK_KP3: c = 'n'; break;
4383 case PK_KP4: c = 'h'; break;
4384 case PK_KP5: c = '.'; break;
4385 case PK_KP6: c = 'l'; break;
4386 case PK_KP7: c = 'y'; break;
4387 case PK_KP8: c = 'k'; break;
4388 case PK_KP9: c = 'u'; break;
4389 default: break; /* else gcc warns `enum value not used' */
4390 }
4391 if (c != 0) {
4392 if (c != '.') {
4393 if (modifiers & PKM_CONTROL)
4394 c &= 0x1f;
4395 else if (modifiers & PKM_SHIFT)
4396 c = toupper(c);
4397 }
4398 *p++ = c;
4399 goto done;
4400 }
4401 }
4402
4403 /* Numeric Keypad */
4404 if (PK_ISKEYPAD(keysym)) {
4405 int xkey = 0;
4406
4407 /*
4408 * In VT400 mode, PFn always emits an escape sequence. In
4409 * Linux and tilde modes, this only happens in app keypad mode.
4410 */
4411 if (term->cfg.funky_type == FUNKY_VT400 ||
4412 ((term->cfg.funky_type == FUNKY_LINUX ||
4413 term->cfg.funky_type == FUNKY_TILDE) &&
4414 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4415 switch (keysym) {
4416 case PK_PF1: xkey = 'P'; break;
4417 case PK_PF2: xkey = 'Q'; break;
4418 case PK_PF3: xkey = 'R'; break;
4419 case PK_PF4: xkey = 'S'; break;
4420 default: break; /* else gcc warns `enum value not used' */
4421 }
4422 }
4423 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4424 switch (keysym) {
4425 case PK_KP0: xkey = 'p'; break;
4426 case PK_KP1: xkey = 'q'; break;
4427 case PK_KP2: xkey = 'r'; break;
4428 case PK_KP3: xkey = 's'; break;
4429 case PK_KP4: xkey = 't'; break;
4430 case PK_KP5: xkey = 'u'; break;
4431 case PK_KP6: xkey = 'v'; break;
4432 case PK_KP7: xkey = 'w'; break;
4433 case PK_KP8: xkey = 'x'; break;
4434 case PK_KP9: xkey = 'y'; break;
4435 case PK_KPDECIMAL: xkey = 'n'; break;
4436 case PK_KPENTER: xkey = 'M'; break;
4437 default: break; /* else gcc warns `enum value not used' */
4438 }
4439 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4440 /*
4441 * xterm can't see the layout of the keypad, so it has
4442 * to rely on the X keysyms returned by the keys.
4443 * Hence, we look at the strings here, not the PuTTY
4444 * keysyms (which describe the layout).
4445 */
4446 switch (text[0]) {
4447 case '+':
4448 if (modifiers & PKM_SHIFT)
4449 xkey = 'l';
4450 else
4451 xkey = 'k';
4452 break;
4453 case '/': xkey = 'o'; break;
4454 case '*': xkey = 'j'; break;
4455 case '-': xkey = 'm'; break;
4456 }
4457 } else {
4458 /*
4459 * In all other modes, we try to retain the layout of
4460 * the DEC keypad in application mode.
4461 */
4462 switch (keysym) {
4463 case PK_KPBIGPLUS:
4464 /* This key covers the '-' and ',' keys on a VT220 */
4465 if (modifiers & PKM_SHIFT)
4466 xkey = 'm'; /* VT220 '-' */
4467 else
4468 xkey = 'l'; /* VT220 ',' */
4469 break;
4470 case PK_KPMINUS: xkey = 'm'; break;
4471 case PK_KPCOMMA: xkey = 'l'; break;
4472 default: break; /* else gcc warns `enum value not used' */
4473 }
4474 }
4475 }
4476 if (xkey) {
4477 if (term->vt52_mode) {
4478 if (xkey >= 'P' && xkey <= 'S')
4479 p += sprintf((char *) p, "\x1B%c", xkey);
4480 else
4481 p += sprintf((char *) p, "\x1B?%c", xkey);
4482 } else
4483 p += sprintf((char *) p, "\x1BO%c", xkey);
4484 goto done;
4485 }
4486 /* Not in application mode -- treat the number pad as arrow keys? */
4487 if ((flags & PKF_NUMLOCK) == 0) {
4488 switch (keysym) {
4489 case PK_KP0: keysym = PK_INSERT; break;
4490 case PK_KP1: keysym = PK_END; break;
4491 case PK_KP2: keysym = PK_DOWN; break;
4492 case PK_KP3: keysym = PK_PAGEDOWN; break;
4493 case PK_KP4: keysym = PK_LEFT; break;
4494 case PK_KP5: keysym = PK_REST; break;
4495 case PK_KP6: keysym = PK_RIGHT; break;
4496 case PK_KP7: keysym = PK_HOME; break;
4497 case PK_KP8: keysym = PK_UP; break;
4498 case PK_KP9: keysym = PK_PAGEUP; break;
4499 default: break; /* else gcc warns `enum value not used' */
4500 }
4501 }
4502 }
4503
4504 /* Miscellaneous keys */
4505 switch (keysym) {
4506 case PK_ESCAPE:
4507 *p++ = 0x1b;
4508 goto done;
4509 case PK_BACKSPACE:
4510 if (modifiers == 0)
4511 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4512 else if (modifiers == PKM_SHIFT)
4513 /* We do the opposite of what is configured */
4514 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4515 else break;
4516 goto done;
4517 case PK_TAB:
4518 if (modifiers == 0)
4519 *p++ = 0x09;
4520 else if (modifiers == PKM_SHIFT)
4521 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4522 else break;
4523 goto done;
4524 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4525 case PK_PAUSE:
4526 if (modifiers == PKM_CONTROL)
4527 *p++ = 26;
4528 else break;
4529 goto done;
4530 case PK_RETURN:
4531 case PK_KPENTER: /* Odd keypad modes handled above */
4532 if (modifiers == 0) {
4533 *p++ = 0x0d;
4534 if (term->cr_lf_return)
4535 *p++ = 0x0a;
4536 goto done;
4537 }
4538 default: break; /* else gcc warns `enum value not used' */
4539 }
4540
4541 /* SCO function keys and editing keys */
4542 if (term->cfg.funky_type == FUNKY_SCO) {
4543 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4544 static char const codes[] =
4545 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4546 int index = keysym - PK_F1;
4547
4548 if (modifiers & PKM_SHIFT) index += 12;
4549 if (modifiers & PKM_CONTROL) index += 24;
4550 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4551 goto done;
4552 }
4553 if (PK_ISEDITING(keysym)) {
4554 int xkey = 0;
4555
4556 switch (keysym) {
4557 case PK_DELETE: *p++ = 0x7f; goto done;
4558 case PK_HOME: xkey = 'H'; break;
4559 case PK_INSERT: xkey = 'L'; break;
4560 case PK_END: xkey = 'F'; break;
4561 case PK_PAGEUP: xkey = 'I'; break;
4562 case PK_PAGEDOWN: xkey = 'G'; break;
4563 default: break; /* else gcc warns `enum value not used' */
4564 }
4565 p += sprintf((char *) p, "\x1B[%c", xkey);
4566 }
4567 }
4568
4569 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4570 int code;
4571
4572 if (term->cfg.funky_type == FUNKY_XTERM) {
4573 /* Xterm shuffles these keys, apparently. */
4574 switch (keysym) {
4575 case PK_HOME: keysym = PK_INSERT; break;
4576 case PK_INSERT: keysym = PK_HOME; break;
4577 case PK_DELETE: keysym = PK_END; break;
4578 case PK_END: keysym = PK_PAGEUP; break;
4579 case PK_PAGEUP: keysym = PK_DELETE; break;
4580 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
4581 default: break; /* else gcc warns `enum value not used' */
4582 }
4583 }
4584
4585 /* RXVT Home/End */
4586 if (term->cfg.rxvt_homeend &&
4587 (keysym == PK_HOME || keysym == PK_END)) {
4588 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4589 goto done;
4590 }
4591
4592 if (term->vt52_mode) {
4593 int xkey;
4594
4595 /*
4596 * A real VT52 doesn't have these, and a VT220 doesn't
4597 * send anything for them in VT52 mode.
4598 */
4599 switch (keysym) {
4600 case PK_HOME: xkey = 'H'; break;
4601 case PK_INSERT: xkey = 'L'; break;
4602 case PK_DELETE: xkey = 'M'; break;
4603 case PK_END: xkey = 'E'; break;
4604 case PK_PAGEUP: xkey = 'I'; break;
4605 case PK_PAGEDOWN: xkey = 'G'; break;
4606 default: xkey=0; break; /* else gcc warns `enum value not used'*/
4607 }
4608 p += sprintf((char *) p, "\x1B%c", xkey);
4609 goto done;
4610 }
4611
4612 switch (keysym) {
4613 case PK_HOME: code = 1; break;
4614 case PK_INSERT: code = 2; break;
4615 case PK_DELETE: code = 3; break;
4616 case PK_END: code = 4; break;
4617 case PK_PAGEUP: code = 5; break;
4618 case PK_PAGEDOWN: code = 6; break;
4619 default: code = 0; break; /* else gcc warns `enum value not used' */
4620 }
4621 p += sprintf((char *) p, "\x1B[%d~", code);
4622 goto done;
4623 }
4624
4625 if (PK_ISFKEY(keysym)) {
4626 /* Map Shift+F1-F10 to F11-F20 */
4627 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4628 keysym += 10;
4629 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4630 keysym <= PK_F14) {
4631 /* XXX This overrides the XTERM/VT52 mode below */
4632 int offt = 0;
4633 if (keysym >= PK_F6) offt++;
4634 if (keysym >= PK_F12) offt++;
4635 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4636 'P' + keysym - PK_F1 - offt);
4637 goto done;
4638 }
4639 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4640 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4641 goto done;
4642 }
4643 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4644 if (term->vt52_mode)
4645 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4646 else
4647 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4648 goto done;
4649 }
4650 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4651 goto done;
4652 }
4653
4654 if (PK_ISCURSOR(keysym)) {
4655 int xkey;
4656
4657 switch (keysym) {
4658 case PK_UP: xkey = 'A'; break;
4659 case PK_DOWN: xkey = 'B'; break;
4660 case PK_RIGHT: xkey = 'C'; break;
4661 case PK_LEFT: xkey = 'D'; break;
4662 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
4663 default: xkey = 0; break; /* else gcc warns `enum value not used' */
4664 }
4665 if (term->vt52_mode)
4666 p += sprintf((char *) p, "\x1B%c", xkey);
4667 else {
4668 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4669
4670 /* Useful mapping of Ctrl-arrows */
4671 if (modifiers == PKM_CONTROL)
4672 app_flg = !app_flg;
4673
4674 if (app_flg)
4675 p += sprintf((char *) p, "\x1BO%c", xkey);
4676 else
4677 p += sprintf((char *) p, "\x1B[%c", xkey);
4678 }
4679 goto done;
4680 }
4681
4682 done:
4683 if (p > output || tlen > 0) {
4684 /*
4685 * Interrupt an ongoing paste. I'm not sure
4686 * this is sensible, but for the moment it's
4687 * preferable to having to faff about buffering
4688 * things.
4689 */
4690 term_nopaste(term);
4691
4692 /*
4693 * We need not bother about stdin backlogs
4694 * here, because in GUI PuTTY we can't do
4695 * anything about it anyway; there's no means
4696 * of asking Windows to hold off on KEYDOWN
4697 * messages. We _have_ to buffer everything
4698 * we're sent.
4699 */
4700 term_seen_key_event(term);
4701
4702 if (prependesc) {
4703 #if 0
4704 fprintf(stderr, "sending ESC\n");
4705 #endif
4706 ldisc_send(term->ldisc, "\x1b", 1, 1);
4707 }
4708
4709 if (p > output) {
4710 #if 0
4711 fprintf(stderr, "sending %d bytes:", p - output);
4712 for (i = 0; i < p - output; i++)
4713 fprintf(stderr, " %02x", output[i]);
4714 fprintf(stderr, "\n");
4715 #endif
4716 ldisc_send(term->ldisc, output, p - output, 1);
4717 } else if (tlen > 0) {
4718 #if 0
4719 fprintf(stderr, "sending %d unichars:", tlen);
4720 for (i = 0; i < tlen; i++)
4721 fprintf(stderr, " %04x", (unsigned) text[i]);
4722 fprintf(stderr, "\n");
4723 #endif
4724 luni_send(term->ldisc, text, tlen, 1);
4725 }
4726 }
4727 }
4728
4729 void term_nopaste(Terminal *term)
4730 {
4731 if (term->paste_len == 0)
4732 return;
4733 sfree(term->paste_buffer);
4734 term->paste_buffer = NULL;
4735 term->paste_len = 0;
4736 }
4737
4738 int term_paste_pending(Terminal *term)
4739 {
4740 return term->paste_len != 0;
4741 }
4742
4743 void term_paste(Terminal *term)
4744 {
4745 long now, paste_diff;
4746
4747 if (term->paste_len == 0)
4748 return;
4749
4750 /* Don't wait forever to paste */
4751 if (term->paste_hold) {
4752 now = GETTICKCOUNT();
4753 paste_diff = now - term->last_paste;
4754 if (paste_diff >= 0 && paste_diff < 450)
4755 return;
4756 }
4757 term->paste_hold = 0;
4758
4759 while (term->paste_pos < term->paste_len) {
4760 int n = 0;
4761 while (n + term->paste_pos < term->paste_len) {
4762 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4763 break;
4764 }
4765 if (term->ldisc)
4766 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4767 term->paste_pos += n;
4768
4769 if (term->paste_pos < term->paste_len) {
4770 term->paste_hold = 1;
4771 return;
4772 }
4773 }
4774 sfree(term->paste_buffer);
4775 term->paste_buffer = NULL;
4776 term->paste_len = 0;
4777 }
4778
4779 static void deselect(Terminal *term)
4780 {
4781 term->selstate = NO_SELECTION;
4782 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4783 }
4784
4785 void term_deselect(Terminal *term)
4786 {
4787 deselect(term);
4788 term_update(term);
4789 }
4790
4791 int term_ldisc(Terminal *term, int option)
4792 {
4793 if (option == LD_ECHO)
4794 return term->term_echoing;
4795 if (option == LD_EDIT)
4796 return term->term_editing;
4797 return FALSE;
4798 }
4799
4800 int term_data(Terminal *term, int is_stderr, const char *data, int len)
4801 {
4802 bufchain_add(&term->inbuf, data, len);
4803
4804 if (!term->in_term_out) {
4805 term->in_term_out = TRUE;
4806 term_blink(term, 1);
4807 term_out(term);
4808 term->in_term_out = FALSE;
4809 }
4810
4811 /*
4812 * term_out() always completely empties inbuf. Therefore,
4813 * there's no reason at all to return anything other than zero
4814 * from this function, because there _can't_ be a question of
4815 * the remote side needing to wait until term_out() has cleared
4816 * a backlog.
4817 *
4818 * This is a slightly suboptimal way to deal with SSH2 - in
4819 * principle, the window mechanism would allow us to continue
4820 * to accept data on forwarded ports and X connections even
4821 * while the terminal processing was going slowly - but we
4822 * can't do the 100% right thing without moving the terminal
4823 * processing into a separate thread, and that might hurt
4824 * portability. So we manage stdout buffering the old SSH1 way:
4825 * if the terminal processing goes slowly, the whole SSH
4826 * connection stops accepting data until it's ready.
4827 *
4828 * In practice, I can't imagine this causing serious trouble.
4829 */
4830 return 0;
4831 }
4832
4833 void term_provide_logctx(Terminal *term, void *logctx)
4834 {
4835 term->logctx = logctx;
4836 }