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