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