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