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