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