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