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