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