faq-alternate-localhost: You can now download the fix directly rather than
[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
1b20b972 59const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
60
4eeb7d09 61#define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
887035a5 62const wchar_t sel_nl[] = SEL_NL;
374330e2 63
64/*
3c7366f8 65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
69 */
70#define UCSGET(a, x) \
71 ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \
72 (a)[(x)-1] : (a)[(x)] )
73
74/*
374330e2 75 * Internal prototypes.
76 */
9547a2ac 77static unsigned long *resizeline(unsigned long *, int);
78static unsigned long *lineptr(Terminal *, int, int);
887035a5 79static void do_paint(Terminal *, Context, int);
80static void erase_lots(Terminal *, int, int, int);
81static void swap_screen(Terminal *, int, int, int);
82static void update_sbar(Terminal *);
83static void deselect(Terminal *);
84static void term_print_finish(Terminal *);
37d2a505 85#ifdef OPTIMISE_SCROLL
86static void scroll_display(Terminal *, int, int, int);
87#endif /* OPTIMISE_SCROLL */
374330e2 88
89/*
2371caac 90 * Resize a line to make it `cols' columns wide.
91 */
9547a2ac 92static unsigned long *resizeline(unsigned long *line, int cols)
2371caac 93{
94 int i, oldlen;
95 unsigned long lineattrs;
96
97 if (line[0] != (unsigned long)cols) {
98 /*
99 * This line is the wrong length, which probably means it
100 * hasn't been accessed since a resize. Resize it now.
101 */
102 oldlen = line[0];
103 lineattrs = line[oldlen + 1];
3d88e64d 104 line = sresize(line, 2 + cols, TTYPE);
2371caac 105 line[0] = cols;
106 for (i = oldlen; i < cols; i++)
107 line[i + 1] = ERASE_CHAR;
108 line[cols + 1] = lineattrs & LATTR_MODE;
109 }
110
111 return line;
112}
113
114/*
876e5d5e 115 * Get the number of lines in the scrollback.
116 */
117static int sblines(Terminal *term)
118{
119 int sblines = count234(term->scrollback);
120 if (term->cfg.erase_to_scrollback &&
121 term->alt_which && term->alt_screen) {
122 sblines += term->alt_sblines;
123 }
124 return sblines;
125}
126
127/*
c83de303 128 * Retrieve a line of the screen or of the scrollback, according to
129 * whether the y coordinate is non-negative or negative
130 * (respectively).
131 */
9547a2ac 132static unsigned long *lineptr(Terminal *term, int y, int lineno)
32874aea 133{
2371caac 134 unsigned long *line, *newline;
c83de303 135 tree234 *whichtree;
2371caac 136 int treeindex;
c83de303 137
138 if (y >= 0) {
887035a5 139 whichtree = term->screen;
c83de303 140 treeindex = y;
141 } else {
876e5d5e 142 int altlines = 0;
143 if (term->cfg.erase_to_scrollback &&
144 term->alt_which && term->alt_screen) {
145 altlines = term->alt_sblines;
146 }
147 if (y < -altlines) {
148 whichtree = term->scrollback;
149 treeindex = y + altlines + count234(term->scrollback);
150 } else {
151 whichtree = term->alt_screen;
152 treeindex = y + term->alt_sblines;
153 /* treeindex = y + count234(term->alt_screen); */
154 }
c83de303 155 }
156 line = index234(whichtree, treeindex);
157
158 /* We assume that we don't screw up and retrieve something out of range. */
c83de303 159 assert(line != NULL);
c83de303 160
887035a5 161 newline = resizeline(line, term->cols);
2371caac 162 if (newline != line) {
c83de303 163 delpos234(whichtree, treeindex);
2371caac 164 addpos234(whichtree, newline, treeindex);
04dcfaa2 165 line = newline;
c83de303 166 }
167
32874aea 168 return line + 1;
c83de303 169}
32874aea 170
887035a5 171#define lineptr(x) lineptr(term,x,__LINE__)
172
c83de303 173/*
374330e2 174 * Set up power-on settings for the terminal.
175 */
887035a5 176static void power_on(Terminal *term)
32874aea 177{
887035a5 178 term->curs.x = term->curs.y = 0;
179 term->alt_x = term->alt_y = 0;
180 term->savecurs.x = term->savecurs.y = 0;
181 term->alt_t = term->marg_t = 0;
182 if (term->rows != -1)
183 term->alt_b = term->marg_b = term->rows - 1;
374330e2 184 else
887035a5 185 term->alt_b = term->marg_b = 0;
186 if (term->cols != -1) {
374330e2 187 int i;
887035a5 188 for (i = 0; i < term->cols; i++)
189 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 190 }
64734920 191 term->alt_om = term->dec_om = term->cfg.dec_om;
f278d6f8 192 term->alt_ins = term->insert = FALSE;
193 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
64734920 194 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
f278d6f8 195 term->alt_cset = term->cset = term->save_cset = 0;
196 term->alt_utf = term->utf = term->save_utf = 0;
197 term->utf_state = 0;
198 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
199 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
887035a5 200 term->rvideo = 0;
201 term->in_vbell = FALSE;
202 term->cursor_on = 1;
203 term->big_cursor = 0;
69761fd5 204 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
887035a5 205 term->term_editing = term->term_echoing = FALSE;
64734920 206 term->app_cursor_keys = term->cfg.app_cursor;
207 term->app_keypad_keys = term->cfg.app_keypad;
208 term->use_bce = term->cfg.bce;
209 term->blink_is_real = term->cfg.blinktext;
887035a5 210 term->erase_char = ERASE_CHAR;
211 term->alt_which = 0;
212 term_print_finish(term);
374330e2 213 {
214 int i;
215 for (i = 0; i < 256; i++)
64734920 216 term->wordness[i] = term->cfg.wordness[i];
374330e2 217 }
887035a5 218 if (term->screen) {
219 swap_screen(term, 1, FALSE, FALSE);
220 erase_lots(term, FALSE, TRUE, TRUE);
221 swap_screen(term, 0, FALSE, FALSE);
222 erase_lots(term, FALSE, TRUE, TRUE);
374330e2 223 }
224}
225
226/*
227 * Force a screen update.
228 */
887035a5 229void term_update(Terminal *term)
32874aea 230{
374330e2 231 Context ctx;
a8327734 232 ctx = get_ctx(term->frontend);
374330e2 233 if (ctx) {
887035a5 234 int need_sbar_update = term->seen_disp_event;
64734920 235 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
887035a5 236 term->disptop = 0; /* return to main screen */
237 term->seen_disp_event = 0;
4f892125 238 need_sbar_update = TRUE;
cabfd08c 239 }
f0fccd51 240
241 /* Allocate temporary buffers for Arabic shaping and bidi. */
242 if (!term->cfg.arabicshaping || !term->cfg.bidi)
243 {
244 term->wcFrom = sresize(term->wcFrom, term->cols, bidi_char);
245 term->ltemp = sresize(term->ltemp, term->cols+1, unsigned long);
246 term->wcTo = sresize(term->wcTo, term->cols, bidi_char);
247 }
248
4f892125 249 if (need_sbar_update)
887035a5 250 update_sbar(term);
251 do_paint(term, ctx, TRUE);
a8327734 252 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
32874aea 253 free_ctx(ctx);
374330e2 254 }
255}
256
257/*
2cb50250 258 * Called from front end when a keypress occurs, to trigger
259 * anything magical that needs to happen in that situation.
260 */
887035a5 261void term_seen_key_event(Terminal *term)
2cb50250 262{
263 /*
264 * On any keypress, clear the bell overload mechanism
265 * completely, on the grounds that large numbers of
266 * beeps coming from deliberate key action are likely
267 * to be intended (e.g. beeps from filename completion
268 * blocking repeatedly).
269 */
887035a5 270 term->beep_overloaded = FALSE;
271 while (term->beephead) {
272 struct beeptime *tmp = term->beephead;
273 term->beephead = tmp->next;
2cb50250 274 sfree(tmp);
275 }
887035a5 276 term->beeptail = NULL;
277 term->nbeeps = 0;
2cb50250 278
279 /*
280 * Reset the scrollback on keypress, if we're doing that.
281 */
64734920 282 if (term->cfg.scroll_on_key) {
887035a5 283 term->disptop = 0; /* return to main screen */
284 term->seen_disp_event = 1;
2cb50250 285 }
286}
287
288/*
374330e2 289 * Same as power_on(), but an external function.
290 */
887035a5 291void term_pwron(Terminal *term)
32874aea 292{
887035a5 293 power_on(term);
b9d7bcad 294 if (term->ldisc) /* cause ldisc to notice changes */
295 ldisc_send(term->ldisc, NULL, 0, 0);
374330e2 296 fix_cpos;
887035a5 297 term->disptop = 0;
298 deselect(term);
299 term_update(term);
374330e2 300}
301
302/*
0d2086c5 303 * When the user reconfigures us, we need to check the forbidden-
b44b307a 304 * alternate-screen config option, disable raw mouse mode if the
305 * user has disabled mouse reporting, and abandon a print job if
306 * the user has disabled printing.
0d2086c5 307 */
64734920 308void term_reconfig(Terminal *term, Config *cfg)
0d2086c5 309{
64734920 310 /*
311 * Before adopting the new config, check all those terminal
312 * settings which control power-on defaults; and if they've
313 * changed, we will modify the current state as well as the
314 * default one. The full list is: Auto wrap mode, DEC Origin
315 * Mode, BCE, blinking text, character classes.
316 */
317 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
318 int i;
319
320 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
321 reset_decom = (term->cfg.dec_om != cfg->dec_om);
322 reset_bce = (term->cfg.bce != cfg->bce);
323 reset_blink = (term->cfg.blinktext != cfg->blinktext);
324 reset_charclass = 0;
325 for (i = 0; i < lenof(term->cfg.wordness); i++)
326 if (term->cfg.wordness[i] != cfg->wordness[i])
327 reset_charclass = 1;
328
329 term->cfg = *cfg; /* STRUCTURE COPY */
330
331 if (reset_wrap)
332 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
333 if (reset_decom)
334 term->alt_om = term->dec_om = term->cfg.dec_om;
7d5c3da4 335 if (reset_bce) {
64734920 336 term->use_bce = term->cfg.bce;
7d5c3da4 337 if (term->use_bce)
338 term->erase_char = (' ' | ATTR_ASCII |
339 (term->curr_attr &
340 (ATTR_FGMASK | ATTR_BGMASK)));
341 else
342 term->erase_char = ERASE_CHAR;
343 }
64734920 344 if (reset_blink)
345 term->blink_is_real = term->cfg.blinktext;
346 if (reset_charclass)
347 for (i = 0; i < 256; i++)
348 term->wordness[i] = term->cfg.wordness[i];
349
350 if (term->cfg.no_alt_screen)
887035a5 351 swap_screen(term, 0, FALSE, FALSE);
64734920 352 if (term->cfg.no_mouse_rep) {
887035a5 353 term->xterm_mouse = 0;
a8327734 354 set_raw_mouse_mode(term->frontend, 0);
c0d36a72 355 }
64734920 356 if (term->cfg.no_remote_charset) {
887035a5 357 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
358 term->sco_acs = term->alt_sco_acs = 0;
359 term->utf = 0;
0d2086c5 360 }
64734920 361 if (!*term->cfg.printer) {
887035a5 362 term_print_finish(term);
b44b307a 363 }
0d2086c5 364}
365
366/*
374330e2 367 * Clear the scrollback.
368 */
887035a5 369void term_clrsb(Terminal *term)
32874aea 370{
4facdf84 371 unsigned long *line;
887035a5 372 term->disptop = 0;
373 while ((line = delpos234(term->scrollback, 0)) != NULL) {
4facdf84 374 sfree(line);
375 }
4683b9b6 376 term->tempsblines = 0;
876e5d5e 377 term->alt_sblines = 0;
887035a5 378 update_sbar(term);
374330e2 379}
380
381/*
382 * Initialise the terminal.
383 */
21d2b241 384Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
385 void *frontend)
32874aea 386{
887035a5 387 Terminal *term;
388
389 /*
390 * Allocate a new Terminal structure and initialise the fields
391 * that need it.
392 */
3d88e64d 393 term = snew(Terminal);
a8327734 394 term->frontend = frontend;
21d2b241 395 term->ucsdata = ucsdata;
64734920 396 term->cfg = *mycfg; /* STRUCTURE COPY */
a8327734 397 term->logctx = NULL;
887035a5 398 term->compatibility_level = TM_PUTTY;
399 strcpy(term->id_string, "\033[?6c");
400 term->last_blink = term->last_tblink = 0;
401 term->paste_buffer = NULL;
c80a3e8c 402 term->paste_len = 0;
887035a5 403 term->last_paste = 0;
404 bufchain_init(&term->inbuf);
405 bufchain_init(&term->printer_buf);
f278d6f8 406 term->printing = term->only_printing = FALSE;
407 term->print_job = NULL;
408 term->vt52_mode = FALSE;
409 term->cr_lf_return = FALSE;
410 term->seen_disp_event = FALSE;
b9d7bcad 411 term->xterm_mouse = term->mouse_is_down = FALSE;
f278d6f8 412 term->reset_132 = FALSE;
413 term->blinker = term->tblinker = 0;
414 term->has_focus = 1;
415 term->repeat_off = FALSE;
416 term->termstate = TOPLEVEL;
417 term->selstate = NO_SELECTION;
093a75b8 418 term->curstype = 0;
887035a5 419
420 term->screen = term->alt_screen = term->scrollback = NULL;
4683b9b6 421 term->tempsblines = 0;
876e5d5e 422 term->alt_sblines = 0;
887035a5 423 term->disptop = 0;
424 term->disptext = term->dispcurs = NULL;
425 term->tabs = NULL;
426 deselect(term);
427 term->rows = term->cols = -1;
428 power_on(term);
429 term->beephead = term->beeptail = NULL;
341eb978 430#ifdef OPTIMISE_SCROLL
431 term->scrollhead = term->scrolltail = NULL;
432#endif /* OPTIMISE_SCROLL */
887035a5 433 term->nbeeps = 0;
434 term->lastbeep = FALSE;
435 term->beep_overloaded = FALSE;
2647b103 436 term->attr_mask = 0xffffffff;
51470298 437 term->resize_fn = NULL;
438 term->resize_ctx = NULL;
74aca06d 439 term->in_term_out = FALSE;
f0fccd51 440 term->ltemp = NULL;
441 term->wcFrom = NULL;
442 term->wcTo = NULL;
443
444 term->bidi_cache_size = 0;
445 term->pre_bidi_cache = term->post_bidi_cache = NULL;
887035a5 446
447 return term;
374330e2 448}
449
fabd1805 450void term_free(Terminal *term)
451{
452 unsigned long *line;
453 struct beeptime *beep;
f0fccd51 454 int i;
fabd1805 455
456 while ((line = delpos234(term->scrollback, 0)) != NULL)
457 sfree(line);
458 freetree234(term->scrollback);
459 while ((line = delpos234(term->screen, 0)) != NULL)
460 sfree(line);
461 freetree234(term->screen);
462 while ((line = delpos234(term->alt_screen, 0)) != NULL)
463 sfree(line);
464 freetree234(term->alt_screen);
465 sfree(term->disptext);
466 while (term->beephead) {
467 beep = term->beephead;
468 term->beephead = beep->next;
469 sfree(beep);
470 }
471 bufchain_clear(&term->inbuf);
472 if(term->print_job)
473 printer_finish_job(term->print_job);
474 bufchain_clear(&term->printer_buf);
475 sfree(term->paste_buffer);
f0fccd51 476 sfree(term->ltemp);
477 sfree(term->wcFrom);
478 sfree(term->wcTo);
479
480 for (i = 0; i < term->bidi_cache_size; i++) {
481 sfree(term->pre_bidi_cache[i]);
482 sfree(term->post_bidi_cache[i]);
483 }
484 sfree(term->pre_bidi_cache);
485 sfree(term->post_bidi_cache);
486
fabd1805 487 sfree(term);
488}
489
374330e2 490/*
491 * Set up the terminal for a given size.
492 */
887035a5 493void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
32874aea 494{
2d466ffd 495 tree234 *newalt;
496 unsigned long *newdisp, *line;
497 int i, j;
c83de303 498 int sblen;
887035a5 499 int save_alt_which = term->alt_which;
d6832394 500
887035a5 501 if (newrows == term->rows && newcols == term->cols &&
502 newsavelines == term->savelines)
374330e2 503 return; /* nothing to do */
504
887035a5 505 deselect(term);
506 swap_screen(term, 0, FALSE, FALSE);
d6832394 507
887035a5 508 term->alt_t = term->marg_t = 0;
509 term->alt_b = term->marg_b = newrows - 1;
374330e2 510
887035a5 511 if (term->rows == -1) {
512 term->scrollback = newtree234(NULL);
513 term->screen = newtree234(NULL);
4683b9b6 514 term->tempsblines = 0;
887035a5 515 term->rows = 0;
c83de303 516 }
517
518 /*
519 * Resize the screen and scrollback. We only need to shift
520 * lines around within our data structures, because lineptr()
521 * will take care of resizing each individual line if
522 * necessary. So:
523 *
4683b9b6 524 * - If the new screen is longer, we shunt lines in from temporary
525 * scrollback if possible, otherwise we add new blank lines at
526 * the bottom.
527 *
528 * - If the new screen is shorter, we remove any blank lines at
529 * the bottom if possible, otherwise shunt lines above the cursor
530 * to scrollback if possible, otherwise delete lines below the
531 * cursor.
c83de303 532 *
533 * - Then, if the new scrollback length is less than the
534 * amount of scrollback we actually have, we must throw some
535 * away.
536 */
887035a5 537 sblen = count234(term->scrollback);
cdd6c586 538 /* Do this loop to expand the screen if newrows > rows */
4683b9b6 539 assert(term->rows == count234(term->screen));
540 while (term->rows < newrows) {
541 if (term->tempsblines > 0) {
542 /* Insert a line from the scrollback at the top of the screen. */
543 assert(sblen >= term->tempsblines);
887035a5 544 line = delpos234(term->scrollback, --sblen);
4683b9b6 545 term->tempsblines -= 1;
546 addpos234(term->screen, line, 0);
547 term->curs.y += 1;
548 term->savecurs.y += 1;
32874aea 549 } else {
4683b9b6 550 /* Add a new blank line at the bottom of the screen. */
3d88e64d 551 line = snewn(newcols + 2, TTYPE);
32874aea 552 line[0] = newcols;
37ca32ed 553 for (j = 0; j < newcols; j++)
32874aea 554 line[j + 1] = ERASE_CHAR;
8ceabe90 555 line[newcols + 1] = LATTR_NORM;
4683b9b6 556 addpos234(term->screen, line, count234(term->screen));
32874aea 557 }
4683b9b6 558 term->rows += 1;
cdd6c586 559 }
560 /* Do this loop to shrink the screen if newrows < rows */
4683b9b6 561 while (term->rows > newrows) {
562 if (term->curs.y < term->rows - 1) {
563 /* delete bottom row, unless it contains the cursor */
564 sfree(delpos234(term->screen, term->rows - 1));
565 } else {
566 /* push top row to scrollback */
567 line = delpos234(term->screen, 0);
568 addpos234(term->scrollback, line, sblen++);
569 term->tempsblines += 1;
570 term->curs.y -= 1;
571 term->savecurs.y -= 1;
572 }
573 term->rows -= 1;
c83de303 574 }
4683b9b6 575 assert(term->rows == newrows);
887035a5 576 assert(count234(term->screen) == newrows);
4683b9b6 577
578 /* Delete any excess lines from the scrollback. */
c83de303 579 while (sblen > newsavelines) {
887035a5 580 line = delpos234(term->scrollback, 0);
c83de303 581 sfree(line);
582 sblen--;
c9def1b8 583 }
4683b9b6 584 if (sblen < term->tempsblines)
585 term->tempsblines = sblen;
887035a5 586 assert(count234(term->scrollback) <= newsavelines);
4683b9b6 587 assert(count234(term->scrollback) >= term->tempsblines);
887035a5 588 term->disptop = 0;
374330e2 589
4683b9b6 590 /* Make a new displayed text buffer. */
3d88e64d 591 newdisp = snewn(newrows * (newcols + 1), TTYPE);
32874aea 592 for (i = 0; i < newrows * (newcols + 1); i++)
374330e2 593 newdisp[i] = ATTR_INVALID;
887035a5 594 sfree(term->disptext);
595 term->disptext = newdisp;
596 term->dispcurs = NULL;
374330e2 597
4683b9b6 598 /* Make a new alternate screen. */
4facdf84 599 newalt = newtree234(NULL);
32874aea 600 for (i = 0; i < newrows; i++) {
3d88e64d 601 line = snewn(newcols + 2, TTYPE);
c83de303 602 line[0] = newcols;
37ca32ed 603 for (j = 0; j < newcols; j++)
887035a5 604 line[j + 1] = term->erase_char;
37ca32ed 605 line[newcols + 1] = LATTR_NORM;
4facdf84 606 addpos234(newalt, line, i);
607 }
887035a5 608 if (term->alt_screen) {
609 while (NULL != (line = delpos234(term->alt_screen, 0)))
4facdf84 610 sfree(line);
887035a5 611 freetree234(term->alt_screen);
4facdf84 612 }
887035a5 613 term->alt_screen = newalt;
876e5d5e 614 term->alt_sblines = 0;
374330e2 615
3d88e64d 616 term->tabs = sresize(term->tabs, newcols, unsigned char);
374330e2 617 {
618 int i;
887035a5 619 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
620 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 621 }
622
4683b9b6 623 /* Check that the cursor positions are still valid. */
624 if (term->savecurs.y < 0)
625 term->savecurs.y = 0;
626 if (term->savecurs.y >= newrows)
627 term->savecurs.y = newrows - 1;
887035a5 628 if (term->curs.y < 0)
629 term->curs.y = 0;
630 if (term->curs.y >= newrows)
631 term->curs.y = newrows - 1;
632 if (term->curs.x >= newcols)
633 term->curs.x = newcols - 1;
634 term->alt_x = term->alt_y = 0;
635 term->wrapnext = term->alt_wnext = FALSE;
636
637 term->rows = newrows;
638 term->cols = newcols;
639 term->savelines = newsavelines;
374330e2 640 fix_cpos;
641
887035a5 642 swap_screen(term, save_alt_which, FALSE, FALSE);
d6832394 643
887035a5 644 update_sbar(term);
645 term_update(term);
51470298 646 if (term->resize_fn)
647 term->resize_fn(term->resize_ctx, term->cols, term->rows);
648}
649
650/*
651 * Hand a function and context pointer to the terminal which it can
652 * use to notify a back end of resizes.
653 */
654void term_provide_resize_fn(Terminal *term,
655 void (*resize_fn)(void *, int, int),
656 void *resize_ctx)
657{
658 term->resize_fn = resize_fn;
659 term->resize_ctx = resize_ctx;
660 if (term->cols > 0 && term->rows > 0)
661 resize_fn(resize_ctx, term->cols, term->rows);
374330e2 662}
663
876e5d5e 664/* Find the bottom line on the screen that has any content.
665 * If only the top line has content, returns 0.
666 * If no lines have content, return -1.
667 */
668static int find_last_nonempty_line(Terminal * term, tree234 * screen)
669{
670 int i;
671 for (i = count234(screen) - 1; i >= 0; i--) {
672 unsigned long *line = index234(screen, i);
673 int j;
674 int cols = line[0];
675 for (j = 0; j < cols; j++) {
676 if (line[j + 1] != term->erase_char) break;
677 }
678 if (j != cols) break;
679 }
680 return i;
681}
682
374330e2 683/*
8f9b6a1a 684 * Swap screens. If `reset' is TRUE and we have been asked to
685 * switch to the alternate screen, we must bring most of its
686 * configuration from the main screen and erase the contents of the
687 * alternate screen completely. (This is even true if we're already
688 * on it! Blame xterm.)
374330e2 689 */
887035a5 690static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
32874aea 691{
374330e2 692 int t;
4facdf84 693 tree234 *ttr;
374330e2 694
8f9b6a1a 695 if (!which)
696 reset = FALSE; /* do no weird resetting if which==0 */
697
887035a5 698 if (which != term->alt_which) {
699 term->alt_which = which;
8f9b6a1a 700
887035a5 701 ttr = term->alt_screen;
702 term->alt_screen = term->screen;
703 term->screen = ttr;
876e5d5e 704 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
887035a5 705 t = term->curs.x;
8f9b6a1a 706 if (!reset && !keep_cur_pos)
887035a5 707 term->curs.x = term->alt_x;
708 term->alt_x = t;
709 t = term->curs.y;
8f9b6a1a 710 if (!reset && !keep_cur_pos)
887035a5 711 term->curs.y = term->alt_y;
712 term->alt_y = t;
713 t = term->marg_t;
714 if (!reset) term->marg_t = term->alt_t;
715 term->alt_t = t;
716 t = term->marg_b;
717 if (!reset) term->marg_b = term->alt_b;
718 term->alt_b = t;
719 t = term->dec_om;
720 if (!reset) term->dec_om = term->alt_om;
721 term->alt_om = t;
722 t = term->wrap;
723 if (!reset) term->wrap = term->alt_wrap;
724 term->alt_wrap = t;
725 t = term->wrapnext;
726 if (!reset) term->wrapnext = term->alt_wnext;
727 term->alt_wnext = t;
728 t = term->insert;
729 if (!reset) term->insert = term->alt_ins;
730 term->alt_ins = t;
731 t = term->cset;
732 if (!reset) term->cset = term->alt_cset;
733 term->alt_cset = t;
734 t = term->utf;
735 if (!reset) term->utf = term->alt_utf;
736 term->alt_utf = t;
737 t = term->sco_acs;
738 if (!reset) term->sco_acs = term->alt_sco_acs;
739 term->alt_sco_acs = t;
8f9b6a1a 740 }
374330e2 741
887035a5 742 if (reset && term->screen) {
8f9b6a1a 743 /*
744 * Yes, this _is_ supposed to honour background-colour-erase.
745 */
887035a5 746 erase_lots(term, FALSE, TRUE, TRUE);
8f9b6a1a 747 }
748
749 /*
750 * This might not be possible if we're called during
751 * initialisation.
752 */
887035a5 753 if (term->screen)
8f9b6a1a 754 fix_cpos;
374330e2 755}
756
757/*
374330e2 758 * Update the scroll bar.
759 */
887035a5 760static void update_sbar(Terminal *term)
32874aea 761{
876e5d5e 762 int nscroll = sblines(term);
a8327734 763 set_sbar(term->frontend, nscroll + term->rows,
764 nscroll + term->disptop, term->rows);
374330e2 765}
766
767/*
768 * Check whether the region bounded by the two pointers intersects
769 * the scroll region, and de-select the on-screen selection if so.
770 */
887035a5 771static void check_selection(Terminal *term, pos from, pos to)
32874aea 772{
887035a5 773 if (poslt(from, term->selend) && poslt(term->selstart, to))
774 deselect(term);
374330e2 775}
776
777/*
778 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
779 * for backward.) `sb' is TRUE if the scrolling is permitted to
780 * affect the scrollback buffer.
a4450583 781 *
782 * NB this function invalidates all pointers into lines of the
783 * screen data structures. In particular, you MUST call fix_cpos
784 * after calling scroll() and before doing anything else that
785 * uses the cpos shortcut pointer.
374330e2 786 */
887035a5 787static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
32874aea 788{
4facdf84 789 unsigned long *line, *line2;
7c66d7d9 790 int i, seltop, olddisptop, shift;
c9def1b8 791
887035a5 792 if (topline != 0 || term->alt_which != 0)
4facdf84 793 sb = FALSE;
794
7c66d7d9 795 olddisptop = term->disptop;
796 shift = lines;
4facdf84 797 if (lines < 0) {
798 while (lines < 0) {
887035a5 799 line = delpos234(term->screen, botline);
800 line = resizeline(line, term->cols);
801 for (i = 0; i < term->cols; i++)
802 line[i + 1] = term->erase_char;
803 line[term->cols + 1] = 0;
804 addpos234(term->screen, line, topline);
805
806 if (term->selstart.y >= topline && term->selstart.y <= botline) {
807 term->selstart.y++;
808 if (term->selstart.y > botline) {
5a8afc78 809 term->selstart.y = botline + 1;
887035a5 810 term->selstart.x = 0;
c9def1b8 811 }
c9def1b8 812 }
887035a5 813 if (term->selend.y >= topline && term->selend.y <= botline) {
814 term->selend.y++;
815 if (term->selend.y > botline) {
5a8afc78 816 term->selend.y = botline + 1;
887035a5 817 term->selend.x = 0;
c9def1b8 818 }
c9def1b8 819 }
c9def1b8 820
4facdf84 821 lines++;
c9def1b8 822 }
374330e2 823 } else {
4facdf84 824 while (lines > 0) {
887035a5 825 line = delpos234(term->screen, topline);
826 if (sb && term->savelines > 0) {
4683b9b6 827 int sblen = count234(term->scrollback);
4facdf84 828 /*
829 * We must add this line to the scrollback. We'll
830 * remove a line from the top of the scrollback to
831 * replace it, or allocate a new one if the
832 * scrollback isn't full.
833 */
887035a5 834 if (sblen == term->savelines) {
835 sblen--, line2 = delpos234(term->scrollback, 0);
c83de303 836 } else {
3d88e64d 837 line2 = snewn(term->cols + 2, TTYPE);
887035a5 838 line2[0] = term->cols;
4683b9b6 839 term->tempsblines += 1;
c83de303 840 }
887035a5 841 addpos234(term->scrollback, line, sblen);
4facdf84 842 line = line2;
a792432e 843
844 /*
845 * If the user is currently looking at part of the
846 * scrollback, and they haven't enabled any options
847 * that are going to reset the scrollback as a
848 * result of this movement, then the chances are
849 * they'd like to keep looking at the same line. So
850 * we move their viewpoint at the same rate as the
851 * scroll, at least until their viewpoint hits the
852 * top end of the scrollback buffer, at which point
853 * we don't have the choice any more.
854 *
855 * Thanks to Jan Holmen Holsten for the idea and
856 * initial implementation.
857 */
887035a5 858 if (term->disptop > -term->savelines && term->disptop < 0)
859 term->disptop--;
4facdf84 860 }
887035a5 861 line = resizeline(line, term->cols);
862 for (i = 0; i < term->cols; i++)
863 line[i + 1] = term->erase_char;
37ca32ed 864 line[term->cols + 1] = LATTR_NORM;
887035a5 865 addpos234(term->screen, line, botline);
4facdf84 866
a792432e 867 /*
868 * If the selection endpoints move into the scrollback,
869 * we keep them moving until they hit the top. However,
870 * of course, if the line _hasn't_ moved into the
871 * scrollback then we don't do this, and cut them off
872 * at the top of the scroll region.
d48db8b4 873 *
874 * This applies to selstart and selend (for an existing
875 * selection), and also selanchor (for one being
876 * selected as we speak).
a792432e 877 */
887035a5 878 seltop = sb ? -term->savelines : topline;
879
c989dbc4 880 if (term->selstate != NO_SELECTION) {
881 if (term->selstart.y >= seltop &&
882 term->selstart.y <= botline) {
883 term->selstart.y--;
884 if (term->selstart.y < seltop) {
885 term->selstart.y = seltop;
886 term->selstart.x = 0;
887 }
4facdf84 888 }
c989dbc4 889 if (term->selend.y >= seltop && term->selend.y <= botline) {
890 term->selend.y--;
891 if (term->selend.y < seltop) {
892 term->selend.y = seltop;
893 term->selend.x = 0;
894 }
4facdf84 895 }
c989dbc4 896 if (term->selanchor.y >= seltop &&
897 term->selanchor.y <= botline) {
898 term->selanchor.y--;
899 if (term->selanchor.y < seltop) {
900 term->selanchor.y = seltop;
901 term->selanchor.x = 0;
902 }
d48db8b4 903 }
904 }
4facdf84 905
906 lines--;
c9def1b8 907 }
374330e2 908 }
7c66d7d9 909#ifdef OPTIMISE_SCROLL
910 shift += term->disptop - olddisptop;
911 if (shift < term->rows && shift > -term->rows && shift != 0)
912 scroll_display(term, topline, botline, shift);
913#endif /* OPTIMISE_SCROLL */
374330e2 914}
915
37d2a505 916#ifdef OPTIMISE_SCROLL
917/*
341eb978 918 * Add a scroll of a region on the screen into the pending scroll list.
919 * `lines' is +ve for scrolling forward, -ve for backward.
920 *
921 * If the scroll is on the same area as the last scroll in the list,
922 * merge them.
923 */
03f60a62 924static void save_scroll(Terminal *term, int topline, int botline, int lines)
341eb978 925{
926 struct scrollregion *newscroll;
927 if (term->scrolltail &&
928 term->scrolltail->topline == topline &&
929 term->scrolltail->botline == botline) {
930 term->scrolltail->lines += lines;
931 } else {
3d88e64d 932 newscroll = snew(struct scrollregion);
341eb978 933 newscroll->topline = topline;
934 newscroll->botline = botline;
935 newscroll->lines = lines;
936 newscroll->next = NULL;
937
938 if (!term->scrollhead)
939 term->scrollhead = newscroll;
940 else
941 term->scrolltail->next = newscroll;
942 term->scrolltail = newscroll;
943 }
944}
945
946/*
37d2a505 947 * Scroll the physical display, and our conception of it in disptext.
948 */
949static void scroll_display(Terminal *term, int topline, int botline, int lines)
950{
951 unsigned long *start, *end;
952 int distance, size, i;
953
954 start = term->disptext + topline * (term->cols + 1);
955 end = term->disptext + (botline + 1) * (term->cols + 1);
956 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
957 size = end - start - distance;
958 if (lines > 0) {
959 memmove(start, start + distance, size * TSIZE);
7c66d7d9 960 if (term->dispcurs >= start + distance &&
961 term->dispcurs <= start + distance + size)
962 term->dispcurs -= distance;
37d2a505 963 for (i = 0; i < distance; i++)
964 (start + size)[i] |= ATTR_INVALID;
965 } else {
966 memmove(start + distance, start, size * TSIZE);
7c66d7d9 967 if (term->dispcurs >= start && term->dispcurs <= start + size)
968 term->dispcurs += distance;
37d2a505 969 for (i = 0; i < distance; i++)
970 start[i] |= ATTR_INVALID;
971 }
341eb978 972 save_scroll(term, topline, botline, lines);
37d2a505 973}
974#endif /* OPTIMISE_SCROLL */
975
374330e2 976/*
977 * Move the cursor to a given position, clipping at boundaries. We
978 * may or may not want to clip at the scroll margin: marg_clip is 0
979 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
980 * even _being_ outside the margins.
981 */
887035a5 982static void move(Terminal *term, int x, int y, int marg_clip)
32874aea 983{
374330e2 984 if (x < 0)
985 x = 0;
887035a5 986 if (x >= term->cols)
987 x = term->cols - 1;
374330e2 988 if (marg_clip) {
887035a5 989 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
990 y < term->marg_t)
991 y = term->marg_t;
992 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
993 y > term->marg_b)
994 y = term->marg_b;
374330e2 995 }
996 if (y < 0)
997 y = 0;
887035a5 998 if (y >= term->rows)
999 y = term->rows - 1;
1000 term->curs.x = x;
1001 term->curs.y = y;
374330e2 1002 fix_cpos;
887035a5 1003 term->wrapnext = FALSE;
374330e2 1004}
1005
1006/*
1007 * Save or restore the cursor and SGR mode.
1008 */
887035a5 1009static void save_cursor(Terminal *term, int save)
32874aea 1010{
374330e2 1011 if (save) {
887035a5 1012 term->savecurs = term->curs;
1013 term->save_attr = term->curr_attr;
1014 term->save_cset = term->cset;
1015 term->save_utf = term->utf;
1016 term->save_wnext = term->wrapnext;
1017 term->save_csattr = term->cset_attr[term->cset];
1018 term->save_sco_acs = term->sco_acs;
374330e2 1019 } else {
887035a5 1020 term->curs = term->savecurs;
c9def1b8 1021 /* Make sure the window hasn't shrunk since the save */
887035a5 1022 if (term->curs.x >= term->cols)
1023 term->curs.x = term->cols - 1;
1024 if (term->curs.y >= term->rows)
1025 term->curs.y = term->rows - 1;
1026
1027 term->curr_attr = term->save_attr;
1028 term->cset = term->save_cset;
1029 term->utf = term->save_utf;
1030 term->wrapnext = term->save_wnext;
8c028e65 1031 /*
1032 * wrapnext might reset to False if the x position is no
1033 * longer at the rightmost edge.
1034 */
887035a5 1035 if (term->wrapnext && term->curs.x < term->cols-1)
1036 term->wrapnext = FALSE;
1037 term->cset_attr[term->cset] = term->save_csattr;
1038 term->sco_acs = term->save_sco_acs;
374330e2 1039 fix_cpos;
887035a5 1040 if (term->use_bce)
1041 term->erase_char = (' ' | ATTR_ASCII |
1042 (term->curr_attr &
1043 (ATTR_FGMASK | ATTR_BGMASK)));
374330e2 1044 }
1045}
1046
1047/*
3c7366f8 1048 * This function is called before doing _anything_ which affects
1049 * only part of a line of text. It is used to mark the boundary
1050 * between two character positions, and it indicates that some sort
1051 * of effect is going to happen on only one side of that boundary.
1052 *
1053 * The effect of this function is to check whether a CJK
1054 * double-width character is straddling the boundary, and to remove
1055 * it and replace it with two spaces if so. (Of course, one or
1056 * other of those spaces is then likely to be replaced with
1057 * something else again, as a result of whatever happens next.)
1058 *
1059 * Also, if the boundary is at the right-hand _edge_ of the screen,
1060 * it implies something deliberate is being done to the rightmost
1061 * column position; hence we must clear LATTR_WRAPPED2.
1062 *
1063 * The input to the function is the coordinates of the _second_
1064 * character of the pair.
1065 */
1066static void check_boundary(Terminal *term, int x, int y)
1067{
1068 unsigned long *ldata;
1069
1070 /* Validate input coordinates, just in case. */
1071 if (x == 0 || x > term->cols)
1072 return;
1073
1074 ldata = lineptr(y);
1075 if (x == term->cols) {
1076 ldata[x] &= ~LATTR_WRAPPED2;
1077 } else {
1078 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
1079 ldata[x-1] = ldata[x] =
1080 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
1081 }
1082 }
1083}
1084
1085/*
374330e2 1086 * Erase a large portion of the screen: the whole screen, or the
1087 * whole line, or parts thereof.
1088 */
887035a5 1089static void erase_lots(Terminal *term,
1090 int line_only, int from_begin, int to_end)
32874aea 1091{
260f3dec 1092 pos start, end;
4facdf84 1093 int erase_lattr;
4683b9b6 1094 int erasing_lines_from_top = 0;
374330e2 1095
1096 if (line_only) {
887035a5 1097 start.y = term->curs.y;
4facdf84 1098 start.x = 0;
887035a5 1099 end.y = term->curs.y + 1;
4facdf84 1100 end.x = 0;
1101 erase_lattr = FALSE;
374330e2 1102 } else {
4facdf84 1103 start.y = 0;
1104 start.x = 0;
887035a5 1105 end.y = term->rows;
4facdf84 1106 end.x = 0;
1107 erase_lattr = TRUE;
1108 }
1109 if (!from_begin) {
887035a5 1110 start = term->curs;
374330e2 1111 }
4facdf84 1112 if (!to_end) {
887035a5 1113 end = term->curs;
2a6dfe0e 1114 incpos(end);
4facdf84 1115 }
3c7366f8 1116 if (!from_begin || !to_end)
1117 check_boundary(term, term->curs.x, term->curs.y);
887035a5 1118 check_selection(term, start, end);
e14a5a13 1119
1120 /* Clear screen also forces a full window redraw, just in case. */
887035a5 1121 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1122 term_invalidate(term);
e14a5a13 1123
4683b9b6 1124 /* Lines scrolled away shouldn't be brought back on if the terminal
1125 * resizes. */
1126 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1127 erasing_lines_from_top = 1;
1128
1129 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
876e5d5e 1130 /* If it's a whole number of lines, starting at the top, and
1131 * we're fully erasing them, erase by scrolling and keep the
1132 * lines in the scrollback. */
1133 int scrolllines = end.y;
1134 if (end.y == term->rows) {
1135 /* Shrink until we find a non-empty row.*/
1136 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1137 }
1138 if (scrolllines > 0)
1139 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1140 fix_cpos;
1141 } else {
1142 unsigned long *ldata = lineptr(start.y);
1143 while (poslt(start, end)) {
1144 if (start.x == term->cols) {
1145 if (!erase_lattr)
1146 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1147 else
1148 ldata[start.x] = LATTR_NORM;
1149 } else {
1150 ldata[start.x] = term->erase_char;
1151 }
1152 if (incpos(start) && start.y < term->rows)
1153 ldata = lineptr(start.y);
1154 }
4facdf84 1155 }
4683b9b6 1156
1157 /* After an erase of lines from the top of the screen, we shouldn't
1158 * bring the lines back again if the terminal enlarges (since the user or
1159 * application has explictly thrown them away). */
1160 if (erasing_lines_from_top && !(term->alt_which))
1161 term->tempsblines = 0;
374330e2 1162}
1163
1164/*
1165 * Insert or delete characters within the current line. n is +ve if
1166 * insertion is desired, and -ve for deletion.
1167 */
887035a5 1168static void insch(Terminal *term, int n)
32874aea 1169{
374330e2 1170 int dir = (n < 0 ? -1 : +1);
1171 int m;
4facdf84 1172 pos cursplus;
1173 unsigned long *ldata;
374330e2 1174
1175 n = (n < 0 ? -n : n);
887035a5 1176 if (n > term->cols - term->curs.x)
1177 n = term->cols - term->curs.x;
1178 m = term->cols - term->curs.x - n;
1179 cursplus.y = term->curs.y;
1180 cursplus.x = term->curs.x + n;
1181 check_selection(term, term->curs, cursplus);
3c7366f8 1182 check_boundary(term, term->curs.x, term->curs.y);
1183 if (dir < 0)
1184 check_boundary(term, term->curs.x + n, term->curs.y);
887035a5 1185 ldata = lineptr(term->curs.y);
374330e2 1186 if (dir < 0) {
887035a5 1187 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
374330e2 1188 while (n--)
887035a5 1189 ldata[term->curs.x + m++] = term->erase_char;
374330e2 1190 } else {
887035a5 1191 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
374330e2 1192 while (n--)
887035a5 1193 ldata[term->curs.x + n] = term->erase_char;
374330e2 1194 }
1195}
1196
1197/*
1198 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1199 * whether the mode is a DEC private one or a normal one.)
1200 */
887035a5 1201static void toggle_mode(Terminal *term, int mode, int query, int state)
32874aea 1202{
286c6b86 1203 unsigned long ticks;
01c034ad 1204
32874aea 1205 if (query)
1206 switch (mode) {
a263e4f6 1207 case 1: /* DECCKM: application cursor keys */
887035a5 1208 term->app_cursor_keys = state;
32874aea 1209 break;
a263e4f6 1210 case 2: /* DECANM: VT52 mode */
887035a5 1211 term->vt52_mode = !state;
1212 if (term->vt52_mode) {
1213 term->blink_is_real = FALSE;
1214 term->vt52_bold = FALSE;
4eeb7d09 1215 } else {
64734920 1216 term->blink_is_real = term->cfg.blinktext;
4eeb7d09 1217 }
32874aea 1218 break;
a263e4f6 1219 case 3: /* DECCOLM: 80/132 columns */
887035a5 1220 deselect(term);
64734920 1221 if (!term->cfg.no_remote_resize)
a8327734 1222 request_resize(term->frontend, state ? 132 : 80, term->rows);
887035a5 1223 term->reset_132 = state;
7ce3b333 1224 term->alt_t = term->marg_t = 0;
1225 term->alt_b = term->marg_b = term->rows - 1;
1226 move(term, 0, 0, 0);
1227 erase_lots(term, FALSE, TRUE, TRUE);
32874aea 1228 break;
a263e4f6 1229 case 5: /* DECSCNM: reverse video */
32874aea 1230 /*
1231 * Toggle reverse video. If we receive an OFF within the
1232 * visual bell timeout period after an ON, we trigger an
1233 * effective visual bell, so that ESC[?5hESC[?5l will
1234 * always be an actually _visible_ visual bell.
1235 */
f7f27309 1236 ticks = GETTICKCOUNT();
286c6b86 1237 /* turn off a previous vbell to avoid inconsistencies */
887035a5 1238 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1239 term->in_vbell = FALSE;
1240 if (term->rvideo && !state && /* we're turning it off... */
1241 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
286c6b86 1242 /* If there's no vbell timeout already, or this one lasts
1243 * longer, replace vbell_timeout with ours. */
887035a5 1244 if (!term->in_vbell ||
1245 (term->rvbell_startpoint - term->vbell_startpoint <
1246 VBELL_TIMEOUT))
1247 term->vbell_startpoint = term->rvbell_startpoint;
1248 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1249 } else if (!term->rvideo && state) {
32874aea 1250 /* This is an ON, so we notice the time and save it. */
887035a5 1251 term->rvbell_startpoint = ticks;
32874aea 1252 }
887035a5 1253 term->rvideo = state;
1254 term->seen_disp_event = TRUE;
32874aea 1255 if (state)
887035a5 1256 term_update(term);
32874aea 1257 break;
a263e4f6 1258 case 6: /* DECOM: DEC origin mode */
887035a5 1259 term->dec_om = state;
32874aea 1260 break;
a263e4f6 1261 case 7: /* DECAWM: auto wrap */
887035a5 1262 term->wrap = state;
32874aea 1263 break;
a263e4f6 1264 case 8: /* DECARM: auto key repeat */
887035a5 1265 term->repeat_off = !state;
32874aea 1266 break;
a263e4f6 1267 case 10: /* DECEDM: set local edit mode */
887035a5 1268 term->term_editing = state;
b9d7bcad 1269 if (term->ldisc) /* cause ldisc to notice changes */
1270 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 1271 break;
a263e4f6 1272 case 25: /* DECTCEM: enable/disable cursor */
32874aea 1273 compatibility2(OTHER, VT220);
887035a5 1274 term->cursor_on = state;
1275 term->seen_disp_event = TRUE;
32874aea 1276 break;
1277 case 47: /* alternate screen */
1278 compatibility(OTHER);
887035a5 1279 deselect(term);
64734920 1280 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
887035a5 1281 term->disptop = 0;
32874aea 1282 break;
1283 case 1000: /* xterm mouse 1 */
887035a5 1284 term->xterm_mouse = state ? 1 : 0;
a8327734 1285 set_raw_mouse_mode(term->frontend, state);
32874aea 1286 break;
1287 case 1002: /* xterm mouse 2 */
887035a5 1288 term->xterm_mouse = state ? 2 : 0;
a8327734 1289 set_raw_mouse_mode(term->frontend, state);
32874aea 1290 break;
8f9b6a1a 1291 case 1047: /* alternate screen */
1292 compatibility(OTHER);
887035a5 1293 deselect(term);
64734920 1294 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
887035a5 1295 term->disptop = 0;
8f9b6a1a 1296 break;
1297 case 1048: /* save/restore cursor */
4c80361f 1298 if (!term->cfg.no_alt_screen)
1299 save_cursor(term, state);
887035a5 1300 if (!state) term->seen_disp_event = TRUE;
8f9b6a1a 1301 break;
1302 case 1049: /* cursor & alternate screen */
4c80361f 1303 if (state && !term->cfg.no_alt_screen)
887035a5 1304 save_cursor(term, state);
1305 if (!state) term->seen_disp_event = TRUE;
8f9b6a1a 1306 compatibility(OTHER);
887035a5 1307 deselect(term);
64734920 1308 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
4c80361f 1309 if (!state && !term->cfg.no_alt_screen)
887035a5 1310 save_cursor(term, state);
1311 term->disptop = 0;
8f9b6a1a 1312 break;
32874aea 1313 } else
1314 switch (mode) {
a263e4f6 1315 case 4: /* IRM: set insert mode */
32874aea 1316 compatibility(VT102);
887035a5 1317 term->insert = state;
32874aea 1318 break;
a263e4f6 1319 case 12: /* SRM: set echo mode */
887035a5 1320 term->term_echoing = !state;
b9d7bcad 1321 if (term->ldisc) /* cause ldisc to notice changes */
1322 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 1323 break;
a263e4f6 1324 case 20: /* LNM: Return sends ... */
887035a5 1325 term->cr_lf_return = state;
32874aea 1326 break;
a263e4f6 1327 case 34: /* WYULCURM: Make cursor BIG */
4eeb7d09 1328 compatibility2(OTHER, VT220);
887035a5 1329 term->big_cursor = !state;
7594731b 1330 }
374330e2 1331}
1332
1333/*
1334 * Process an OSC sequence: set window title or icon name.
1335 */
887035a5 1336static void do_osc(Terminal *term)
32874aea 1337{
887035a5 1338 if (term->osc_w) {
1339 while (term->osc_strlen--)
1340 term->wordness[(unsigned char)
1341 term->osc_string[term->osc_strlen]] = term->esc_args[0];
374330e2 1342 } else {
887035a5 1343 term->osc_string[term->osc_strlen] = '\0';
1344 switch (term->esc_args[0]) {
374330e2 1345 case 0:
1346 case 1:
64734920 1347 if (!term->cfg.no_remote_wintitle)
a8327734 1348 set_icon(term->frontend, term->osc_string);
887035a5 1349 if (term->esc_args[0] == 1)
374330e2 1350 break;
1351 /* fall through: parameter 0 means set both */
1352 case 2:
1353 case 21:
64734920 1354 if (!term->cfg.no_remote_wintitle)
a8327734 1355 set_title(term->frontend, term->osc_string);
374330e2 1356 break;
1357 }
1358 }
1359}
1360
1361/*
b44b307a 1362 * ANSI printing routines.
1363 */
887035a5 1364static void term_print_setup(Terminal *term)
b44b307a 1365{
887035a5 1366 bufchain_clear(&term->printer_buf);
64734920 1367 term->print_job = printer_start_job(term->cfg.printer);
b44b307a 1368}
887035a5 1369static void term_print_flush(Terminal *term)
b44b307a 1370{
1371 void *data;
1372 int len;
1373 int size;
887035a5 1374 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1375 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 1376 if (len > size-5)
1377 len = size-5;
887035a5 1378 printer_job_data(term->print_job, data, len);
1379 bufchain_consume(&term->printer_buf, len);
b44b307a 1380 }
1381}
887035a5 1382static void term_print_finish(Terminal *term)
b44b307a 1383{
1384 void *data;
1385 int len, size;
1386 char c;
1387
887035a5 1388 if (!term->printing && !term->only_printing)
1709795f 1389 return; /* we need do nothing */
1390
887035a5 1391 term_print_flush(term);
1392 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1393 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 1394 c = *(char *)data;
1395 if (c == '\033' || c == '\233') {
887035a5 1396 bufchain_consume(&term->printer_buf, size);
b44b307a 1397 break;
1398 } else {
887035a5 1399 printer_job_data(term->print_job, &c, 1);
1400 bufchain_consume(&term->printer_buf, 1);
b44b307a 1401 }
1402 }
887035a5 1403 printer_finish_job(term->print_job);
1404 term->print_job = NULL;
1405 term->printing = term->only_printing = FALSE;
b44b307a 1406}
1407
1408/*
374330e2 1409 * Remove everything currently in `inbuf' and stick it up on the
1410 * in-memory display. There's a big state machine in here to
1411 * process escape sequences...
1412 */
887035a5 1413void term_out(Terminal *term)
32874aea 1414{
a748a096 1415 int c, unget;
1416 unsigned char localbuf[256], *chars;
1417 int nchars = 0;
1418
1419 unget = -1;
1420
1709795f 1421 chars = NULL; /* placate compiler warnings */
887035a5 1422 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
a748a096 1423 if (unget == -1) {
1424 if (nchars == 0) {
1425 void *ret;
887035a5 1426 bufchain_prefix(&term->inbuf, &ret, &nchars);
a748a096 1427 if (nchars > sizeof(localbuf))
1428 nchars = sizeof(localbuf);
1429 memcpy(localbuf, ret, nchars);
887035a5 1430 bufchain_consume(&term->inbuf, nchars);
a748a096 1431 chars = localbuf;
1432 assert(chars != NULL);
1433 }
1434 c = *chars++;
1435 nchars--;
c9def1b8 1436
a748a096 1437 /*
1438 * Optionally log the session traffic to a file. Useful for
1439 * debugging and possibly also useful for actual logging.
1440 */
64734920 1441 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
a8327734 1442 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
a748a096 1443 } else {
1444 c = unget;
1445 unget = -1;
5a16642f 1446 }
1447
e14a5a13 1448 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1449 * be able to display 8-bit characters, but I'll let that go 'cause
1450 * of i18n.
1451 */
4eeb7d09 1452
b44b307a 1453 /*
1454 * If we're printing, add the character to the printer
1455 * buffer.
1456 */
887035a5 1457 if (term->printing) {
1458 bufchain_add(&term->printer_buf, &c, 1);
b44b307a 1459
1460 /*
1461 * If we're in print-only mode, we use a much simpler
1462 * state machine designed only to recognise the ESC[4i
1463 * termination sequence.
1464 */
887035a5 1465 if (term->only_printing) {
b44b307a 1466 if (c == '\033')
887035a5 1467 term->print_state = 1;
b44b307a 1468 else if (c == (unsigned char)'\233')
887035a5 1469 term->print_state = 2;
1470 else if (c == '[' && term->print_state == 1)
1471 term->print_state = 2;
1472 else if (c == '4' && term->print_state == 2)
1473 term->print_state = 3;
1474 else if (c == 'i' && term->print_state == 3)
1475 term->print_state = 4;
b44b307a 1476 else
887035a5 1477 term->print_state = 0;
1478 if (term->print_state == 4) {
1479 term_print_finish(term);
b44b307a 1480 }
1481 continue;
1482 }
1483 }
1484
4eeb7d09 1485 /* First see about all those translations. */
887035a5 1486 if (term->termstate == TOPLEVEL) {
1487 if (in_utf(term))
1488 switch (term->utf_state) {
4eeb7d09 1489 case 0:
1490 if (c < 0x80) {
8f22582c 1491 /* UTF-8 must be stateless so we ignore iso2022. */
21d2b241 1492 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1493 c = term->ucsdata->unitab_ctrl[c];
8f22582c 1494 else c = ((unsigned char)c) | ATTR_ASCII;
1495 break;
4eeb7d09 1496 } else if ((c & 0xe0) == 0xc0) {
887035a5 1497 term->utf_size = term->utf_state = 1;
1498 term->utf_char = (c & 0x1f);
4eeb7d09 1499 } else if ((c & 0xf0) == 0xe0) {
887035a5 1500 term->utf_size = term->utf_state = 2;
1501 term->utf_char = (c & 0x0f);
4eeb7d09 1502 } else if ((c & 0xf8) == 0xf0) {
887035a5 1503 term->utf_size = term->utf_state = 3;
1504 term->utf_char = (c & 0x07);
4eeb7d09 1505 } else if ((c & 0xfc) == 0xf8) {
887035a5 1506 term->utf_size = term->utf_state = 4;
1507 term->utf_char = (c & 0x03);
4eeb7d09 1508 } else if ((c & 0xfe) == 0xfc) {
887035a5 1509 term->utf_size = term->utf_state = 5;
1510 term->utf_char = (c & 0x01);
4eeb7d09 1511 } else {
1512 c = UCSERR;
1513 break;
1514 }
1515 continue;
1516 case 1:
1517 case 2:
1518 case 3:
1519 case 4:
1520 case 5:
1521 if ((c & 0xC0) != 0x80) {
a748a096 1522 unget = c;
5a16642f 1523 c = UCSERR;
887035a5 1524 term->utf_state = 0;
4eeb7d09 1525 break;
1526 }
887035a5 1527 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1528 if (--term->utf_state)
4eeb7d09 1529 continue;
1530
887035a5 1531 c = term->utf_char;
4eeb7d09 1532
1533 /* Is somebody trying to be evil! */
1534 if (c < 0x80 ||
887035a5 1535 (c < 0x800 && term->utf_size >= 2) ||
1536 (c < 0x10000 && term->utf_size >= 3) ||
1537 (c < 0x200000 && term->utf_size >= 4) ||
1538 (c < 0x4000000 && term->utf_size >= 5))
4eeb7d09 1539 c = UCSERR;
1540
1541 /* Unicode line separator and paragraph separator are CR-LF */
1542 if (c == 0x2028 || c == 0x2029)
1543 c = 0x85;
1544
1545 /* High controls are probably a Baaad idea too. */
1546 if (c < 0xA0)
1547 c = 0xFFFD;
1548
1549 /* The UTF-16 surrogates are not nice either. */
1550 /* The standard give the option of decoding these:
1551 * I don't want to! */
1552 if (c >= 0xD800 && c < 0xE000)
1553 c = UCSERR;
1554
1555 /* ISO 10646 characters now limited to UTF-16 range. */
1556 if (c > 0x10FFFF)
1557 c = UCSERR;
1558
1559 /* This is currently a TagPhobic application.. */
1560 if (c >= 0xE0000 && c <= 0xE007F)
1561 continue;
1562
1563 /* U+FEFF is best seen as a null. */
1564 if (c == 0xFEFF)
1565 continue;
1566 /* But U+FFFE is an error. */
1567 if (c == 0xFFFE || c == 0xFFFF)
1568 c = UCSERR;
1569
1570 /* Oops this is a 16bit implementation */
1571 if (c >= 0x10000)
1572 c = 0xFFFD;
1573 break;
d3cb5465 1574 }
1575 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
887035a5 1576 else if(term->sco_acs &&
92e23ad9 1577 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
d3cb5465 1578 {
5dc6132d 1579 if (term->sco_acs == 2) c |= 0x80;
d3cb5465 1580 c |= ATTR_SCOACS;
4eeb7d09 1581 } else {
887035a5 1582 switch (term->cset_attr[term->cset]) {
4eeb7d09 1583 /*
1584 * Linedraw characters are different from 'ESC ( B'
1585 * only for a small range. For ones outside that
1586 * range, make sure we use the same font as well as
1587 * the same encoding.
1588 */
1589 case ATTR_LINEDRW:
21d2b241 1590 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1591 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 1592 else
1593 c = ((unsigned char) c) | ATTR_LINEDRW;
1594 break;
1595
1596 case ATTR_GBCHR:
1597 /* If UK-ASCII, make the '#' a LineDraw Pound */
1598 if (c == '#') {
1599 c = '}' | ATTR_LINEDRW;
1600 break;
1601 }
1602 /*FALLTHROUGH*/ case ATTR_ASCII:
21d2b241 1603 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1604 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 1605 else
1606 c = ((unsigned char) c) | ATTR_ASCII;
1607 break;
d3cb5465 1608 case ATTR_SCOACS:
1609 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1610 break;
4eeb7d09 1611 }
1612 }
1613 }
1614
1615 /* How about C1 controls ? */
887035a5 1616 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1617 !term->vt52_mode && has_compat(VT220)) {
1618 term->termstate = SEEN_ESC;
1619 term->esc_query = FALSE;
4eeb7d09 1620 c = '@' + (c & 0x1F);
1621 }
1622
1623 /* Or the GL control. */
887035a5 1624 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1625 if (term->curs.x && !term->wrapnext)
1626 term->curs.x--;
1627 term->wrapnext = FALSE;
4eeb7d09 1628 fix_cpos;
64734920 1629 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
887035a5 1630 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
4eeb7d09 1631 } else
1632 /* Or normal C0 controls. */
887035a5 1633 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
374330e2 1634 switch (c) {
a263e4f6 1635 case '\005': /* ENQ: terminal type query */
e14a5a13 1636 /* Strictly speaking this is VT100 but a VT100 defaults to
1637 * no response. Other terminals respond at their option.
1638 *
1639 * Don't put a CR in the default string as this tends to
1640 * upset some weird software.
1641 *
1642 * An xterm returns "xterm" (5 characters)
1643 */
32874aea 1644 compatibility(ANSIMIN);
b9d7bcad 1645 if (term->ldisc) {
e7fbcdd8 1646 char abuf[256], *s, *d;
32874aea 1647 int state = 0;
64734920 1648 for (s = term->cfg.answerback, d = abuf; *s; s++) {
32874aea 1649 if (state) {
1650 if (*s >= 'a' && *s <= 'z')
1651 *d++ = (*s - ('a' - 1));
1652 else if ((*s >= '@' && *s <= '_') ||
1653 *s == '?' || (*s & 0x80))
1654 *d++ = ('@' ^ *s);
1655 else if (*s == '~')
1656 *d++ = '^';
1657 state = 0;
1658 } else if (*s == '^') {
1659 state = 1;
1660 } else
4eeb7d09 1661 *d++ = *s;
e7fbcdd8 1662 }
b9d7bcad 1663 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1664 abuf, d - abuf, 0);
e7fbcdd8 1665 }
374330e2 1666 break;
a263e4f6 1667 case '\007': /* BEL: Bell */
156686ef 1668 {
1669 struct beeptime *newbeep;
286c6b86 1670 unsigned long ticks;
156686ef 1671
f7f27309 1672 ticks = GETTICKCOUNT();
156686ef 1673
887035a5 1674 if (!term->beep_overloaded) {
3d88e64d 1675 newbeep = snew(struct beeptime);
156686ef 1676 newbeep->ticks = ticks;
1677 newbeep->next = NULL;
887035a5 1678 if (!term->beephead)
1679 term->beephead = newbeep;
156686ef 1680 else
887035a5 1681 term->beeptail->next = newbeep;
1682 term->beeptail = newbeep;
1683 term->nbeeps++;
156686ef 1684 }
1685
1686 /*
1687 * Throw out any beeps that happened more than
1688 * t seconds ago.
1689 */
887035a5 1690 while (term->beephead &&
64734920 1691 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
887035a5 1692 struct beeptime *tmp = term->beephead;
1693 term->beephead = tmp->next;
156686ef 1694 sfree(tmp);
887035a5 1695 if (!term->beephead)
1696 term->beeptail = NULL;
1697 term->nbeeps--;
156686ef 1698 }
1699
64734920 1700 if (term->cfg.bellovl && term->beep_overloaded &&
1701 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
156686ef 1702 /*
1703 * If we're currently overloaded and the
1704 * last beep was more than s seconds ago,
1705 * leave overload mode.
1706 */
887035a5 1707 term->beep_overloaded = FALSE;
64734920 1708 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1709 term->nbeeps >= term->cfg.bellovl_n) {
156686ef 1710 /*
1711 * Now, if we have n or more beeps
1712 * remaining in the queue, go into overload
1713 * mode.
1714 */
887035a5 1715 term->beep_overloaded = TRUE;
156686ef 1716 }
887035a5 1717 term->lastbeep = ticks;
156686ef 1718
1719 /*
1720 * Perform an actual beep if we're not overloaded.
1721 */
64734920 1722 if (!term->cfg.bellovl || !term->beep_overloaded) {
1723 beep(term->frontend, term->cfg.beep);
1724 if (term->cfg.beep == BELL_VISUAL) {
887035a5 1725 term->in_vbell = TRUE;
1726 term->vbell_startpoint = ticks;
1727 term_update(term);
0ce89525 1728 }
156686ef 1729 }
6b7f4cba 1730 term->seen_disp_event = TRUE;
156686ef 1731 }
374330e2 1732 break;
a263e4f6 1733 case '\b': /* BS: Back space */
887035a5 1734 if (term->curs.x == 0 &&
1735 (term->curs.y == 0 || term->wrap == 0))
1736 /* do nothing */ ;
1737 else if (term->curs.x == 0 && term->curs.y > 0)
1738 term->curs.x = term->cols - 1, term->curs.y--;
1739 else if (term->wrapnext)
1740 term->wrapnext = FALSE;
374330e2 1741 else
887035a5 1742 term->curs.x--;
374330e2 1743 fix_cpos;
887035a5 1744 term->seen_disp_event = TRUE;
374330e2 1745 break;
a263e4f6 1746 case '\016': /* LS1: Locking-shift one */
32874aea 1747 compatibility(VT100);
887035a5 1748 term->cset = 1;
374330e2 1749 break;
a263e4f6 1750 case '\017': /* LS0: Locking-shift zero */
32874aea 1751 compatibility(VT100);
887035a5 1752 term->cset = 0;
374330e2 1753 break;
a263e4f6 1754 case '\033': /* ESC: Escape */
887035a5 1755 if (term->vt52_mode)
1756 term->termstate = VT52_ESC;
e14a5a13 1757 else {
1758 compatibility(ANSIMIN);
887035a5 1759 term->termstate = SEEN_ESC;
1760 term->esc_query = FALSE;
e14a5a13 1761 }
374330e2 1762 break;
a263e4f6 1763 case '\015': /* CR: Carriage return */
887035a5 1764 term->curs.x = 0;
1765 term->wrapnext = FALSE;
374330e2 1766 fix_cpos;
887035a5 1767 term->seen_disp_event = TRUE;
1768 term->paste_hold = 0;
a8327734 1769 if (term->logctx)
1770 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 1771 break;
a263e4f6 1772 case '\014': /* FF: Form feed */
1a837633 1773 if (has_compat(SCOANSI)) {
887035a5 1774 move(term, 0, 0, 0);
1775 erase_lots(term, FALSE, FALSE, TRUE);
1776 term->disptop = 0;
1777 term->wrapnext = FALSE;
1778 term->seen_disp_event = 1;
1a837633 1779 break;
1780 }
a263e4f6 1781 case '\013': /* VT: Line tabulation */
32874aea 1782 compatibility(VT100);
a263e4f6 1783 case '\012': /* LF: Line feed */
887035a5 1784 if (term->curs.y == term->marg_b)
1785 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1786 else if (term->curs.y < term->rows - 1)
1787 term->curs.y++;
64734920 1788 if (term->cfg.lfhascr)
887035a5 1789 term->curs.x = 0;
374330e2 1790 fix_cpos;
887035a5 1791 term->wrapnext = FALSE;
1792 term->seen_disp_event = 1;
1793 term->paste_hold = 0;
a8327734 1794 if (term->logctx)
1795 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 1796 break;
a263e4f6 1797 case '\t': /* HT: Character tabulation */
374330e2 1798 {
887035a5 1799 pos old_curs = term->curs;
1800 unsigned long *ldata = lineptr(term->curs.y);
c9def1b8 1801
1802 do {
887035a5 1803 term->curs.x++;
1804 } while (term->curs.x < term->cols - 1 &&
1805 !term->tabs[term->curs.x]);
c9def1b8 1806
887035a5 1807 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1808 if (term->curs.x >= term->cols / 2)
1809 term->curs.x = term->cols / 2 - 1;
32874aea 1810 } else {
887035a5 1811 if (term->curs.x >= term->cols)
1812 term->curs.x = term->cols - 1;
c9def1b8 1813 }
1814
374330e2 1815 fix_cpos;
887035a5 1816 check_selection(term, old_curs, term->curs);
374330e2 1817 }
887035a5 1818 term->seen_disp_event = TRUE;
374330e2 1819 break;
cabfd08c 1820 }
32874aea 1821 } else
887035a5 1822 switch (term->termstate) {
32874aea 1823 case TOPLEVEL:
887035a5 1824 /* Only graphic characters get this far;
1825 * ctrls are stripped above */
1826 if (term->wrapnext && term->wrap) {
1827 term->cpos[1] |= LATTR_WRAPPED;
1828 if (term->curs.y == term->marg_b)
1829 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1830 else if (term->curs.y < term->rows - 1)
1831 term->curs.y++;
1832 term->curs.x = 0;
32874aea 1833 fix_cpos;
887035a5 1834 term->wrapnext = FALSE;
374330e2 1835 }
887035a5 1836 if (term->insert)
1837 insch(term, 1);
1838 if (term->selstate != NO_SELECTION) {
1839 pos cursplus = term->curs;
32874aea 1840 incpos(cursplus);
887035a5 1841 check_selection(term, term->curs, cursplus);
374330e2 1842 }
a8327734 1843 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1844 term->logctx)
1845 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
4eeb7d09 1846 {
4eeb7d09 1847 int width = 0;
1848 if (DIRECT_CHAR(c))
1849 width = 1;
1850 if (!width)
1851 width = wcwidth((wchar_t) c);
1852 switch (width) {
1853 case 2:
3c7366f8 1854 /*
1855 * If we're about to display a double-width
1856 * character starting in the rightmost
1857 * column, then we do something special
1858 * instead. We must print a space in the
1859 * last column of the screen, then wrap;
1860 * and we also set LATTR_WRAPPED2 which
1861 * instructs subsequent cut-and-pasting not
1862 * only to splice this line to the one
1863 * after it, but to ignore the space in the
1864 * last character position as well.
1865 * (Because what was actually output to the
1866 * terminal was presumably just a sequence
1867 * of CJK characters, and we don't want a
1868 * space to be pasted in the middle of
1869 * those just because they had the
1870 * misfortune to start in the wrong parity
1871 * column. xterm concurs.)
1872 */
1873 check_boundary(term, term->curs.x, term->curs.y);
1874 check_boundary(term, term->curs.x+2, term->curs.y);
1875 if (term->curs.x == term->cols-1) {
1876 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1877 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
887035a5 1878 if (term->curs.y == term->marg_b)
1879 scroll(term, term->marg_t, term->marg_b,
1880 1, TRUE);
1881 else if (term->curs.y < term->rows - 1)
1882 term->curs.y++;
1883 term->curs.x = 0;
5a73255e 1884 fix_cpos;
3c7366f8 1885 /* Now we must check_boundary again, of course. */
1886 check_boundary(term, term->curs.x, term->curs.y);
1887 check_boundary(term, term->curs.x+2, term->curs.y);
4eeb7d09 1888 }
3c7366f8 1889 *term->cpos++ = c | term->curr_attr;
887035a5 1890 *term->cpos++ = UCSWIDE | term->curr_attr;
3c7366f8 1891 term->curs.x++;
5a73255e 1892 break;
4eeb7d09 1893 case 1:
3c7366f8 1894 check_boundary(term, term->curs.x, term->curs.y);
1895 check_boundary(term, term->curs.x+1, term->curs.y);
887035a5 1896 *term->cpos++ = c | term->curr_attr;
32874aea 1897 break;
4eeb7d09 1898 default:
1899 continue;
32874aea 1900 }
374330e2 1901 }
887035a5 1902 term->curs.x++;
1903 if (term->curs.x == term->cols) {
1904 term->cpos--;
1905 term->curs.x--;
1906 term->wrapnext = TRUE;
1907 if (term->wrap && term->vt52_mode) {
1908 term->cpos[1] |= LATTR_WRAPPED;
1909 if (term->curs.y == term->marg_b)
1910 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1911 else if (term->curs.y < term->rows - 1)
1912 term->curs.y++;
1913 term->curs.x = 0;
4eeb7d09 1914 fix_cpos;
887035a5 1915 term->wrapnext = FALSE;
4eeb7d09 1916 }
e14a5a13 1917 }
887035a5 1918 term->seen_disp_event = 1;
374330e2 1919 break;
32874aea 1920
32874aea 1921 case OSC_MAYBE_ST:
1922 /*
1923 * This state is virtually identical to SEEN_ESC, with the
1924 * exception that we have an OSC sequence in the pipeline,
1925 * and _if_ we see a backslash, we process it.
1926 */
1927 if (c == '\\') {
887035a5 1928 do_osc(term);
1929 term->termstate = TOPLEVEL;
32874aea 1930 break;
e14a5a13 1931 }
32874aea 1932 /* else fall through */
1933 case SEEN_ESC:
4eeb7d09 1934 if (c >= ' ' && c <= '/') {
887035a5 1935 if (term->esc_query)
1936 term->esc_query = -1;
4eeb7d09 1937 else
887035a5 1938 term->esc_query = c;
32874aea 1939 break;
4eeb7d09 1940 }
887035a5 1941 term->termstate = TOPLEVEL;
1942 switch (ANSI(c, term->esc_query)) {
a263e4f6 1943 case '[': /* enter CSI mode */
887035a5 1944 term->termstate = SEEN_CSI;
1945 term->esc_nargs = 1;
1946 term->esc_args[0] = ARG_DEFAULT;
1947 term->esc_query = FALSE;
32874aea 1948 break;
a263e4f6 1949 case ']': /* OSC: xterm escape sequences */
32874aea 1950 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1951 compatibility(OTHER);
887035a5 1952 term->termstate = SEEN_OSC;
1953 term->esc_args[0] = 0;
32874aea 1954 break;
a263e4f6 1955 case '7': /* DECSC: save cursor */
32874aea 1956 compatibility(VT100);
887035a5 1957 save_cursor(term, TRUE);
32874aea 1958 break;
a263e4f6 1959 case '8': /* DECRC: restore cursor */
32874aea 1960 compatibility(VT100);
887035a5 1961 save_cursor(term, FALSE);
1962 term->seen_disp_event = TRUE;
32874aea 1963 break;
a263e4f6 1964 case '=': /* DECKPAM: Keypad application mode */
32874aea 1965 compatibility(VT100);
887035a5 1966 term->app_keypad_keys = TRUE;
32874aea 1967 break;
a263e4f6 1968 case '>': /* DECKPNM: Keypad numeric mode */
32874aea 1969 compatibility(VT100);
887035a5 1970 term->app_keypad_keys = FALSE;
32874aea 1971 break;
a263e4f6 1972 case 'D': /* IND: exactly equivalent to LF */
32874aea 1973 compatibility(VT100);
887035a5 1974 if (term->curs.y == term->marg_b)
1975 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1976 else if (term->curs.y < term->rows - 1)
1977 term->curs.y++;
32874aea 1978 fix_cpos;
887035a5 1979 term->wrapnext = FALSE;
1980 term->seen_disp_event = TRUE;
32874aea 1981 break;
a263e4f6 1982 case 'E': /* NEL: exactly equivalent to CR-LF */
32874aea 1983 compatibility(VT100);
887035a5 1984 term->curs.x = 0;
1985 if (term->curs.y == term->marg_b)
1986 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1987 else if (term->curs.y < term->rows - 1)
1988 term->curs.y++;
32874aea 1989 fix_cpos;
887035a5 1990 term->wrapnext = FALSE;
1991 term->seen_disp_event = TRUE;
32874aea 1992 break;
a263e4f6 1993 case 'M': /* RI: reverse index - backwards LF */
32874aea 1994 compatibility(VT100);
887035a5 1995 if (term->curs.y == term->marg_t)
1996 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1997 else if (term->curs.y > 0)
1998 term->curs.y--;
32874aea 1999 fix_cpos;
887035a5 2000 term->wrapnext = FALSE;
2001 term->seen_disp_event = TRUE;
32874aea 2002 break;
a263e4f6 2003 case 'Z': /* DECID: terminal type query */
32874aea 2004 compatibility(VT100);
b9d7bcad 2005 if (term->ldisc)
2006 ldisc_send(term->ldisc, term->id_string,
2007 strlen(term->id_string), 0);
32874aea 2008 break;
a263e4f6 2009 case 'c': /* RIS: restore power-on settings */
32874aea 2010 compatibility(VT100);
887035a5 2011 power_on(term);
b9d7bcad 2012 if (term->ldisc) /* cause ldisc to notice changes */
2013 ldisc_send(term->ldisc, NULL, 0, 0);
887035a5 2014 if (term->reset_132) {
64734920 2015 if (!term->cfg.no_remote_resize)
a8327734 2016 request_resize(term->frontend, 80, term->rows);
887035a5 2017 term->reset_132 = 0;
374330e2 2018 }
32874aea 2019 fix_cpos;
887035a5 2020 term->disptop = 0;
2021 term->seen_disp_event = TRUE;
32874aea 2022 break;
a263e4f6 2023 case 'H': /* HTS: set a tab */
32874aea 2024 compatibility(VT100);
887035a5 2025 term->tabs[term->curs.x] = TRUE;
32874aea 2026 break;
4eeb7d09 2027
a263e4f6 2028 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
4eeb7d09 2029 compatibility(VT100);
2030 {
2031 unsigned long *ldata;
2032 int i, j;
2033 pos scrtop, scrbot;
2034
887035a5 2035 for (i = 0; i < term->rows; i++) {
4eeb7d09 2036 ldata = lineptr(i);
887035a5 2037 for (j = 0; j < term->cols; j++)
4eeb7d09 2038 ldata[j] = ATTR_DEFAULT | 'E';
887035a5 2039 ldata[term->cols] = 0;
4eeb7d09 2040 }
887035a5 2041 term->disptop = 0;
2042 term->seen_disp_event = TRUE;
4eeb7d09 2043 scrtop.x = scrtop.y = 0;
2044 scrbot.x = 0;
887035a5 2045 scrbot.y = term->rows;
2046 check_selection(term, scrtop, scrbot);
4eeb7d09 2047 }
2048 break;
2049
2050 case ANSI('3', '#'):
2051 case ANSI('4', '#'):
2052 case ANSI('5', '#'):
2053 case ANSI('6', '#'):
2054 compatibility(VT100);
2055 {
2056 unsigned long nlattr;
2057 unsigned long *ldata;
887035a5 2058 switch (ANSI(c, term->esc_query)) {
a263e4f6 2059 case ANSI('3', '#'): /* DECDHL: 2*height, top */
4eeb7d09 2060 nlattr = LATTR_TOP;
2061 break;
a263e4f6 2062 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
4eeb7d09 2063 nlattr = LATTR_BOT;
2064 break;
a263e4f6 2065 case ANSI('5', '#'): /* DECSWL: normal */
4eeb7d09 2066 nlattr = LATTR_NORM;
2067 break;
a263e4f6 2068 default: /* case ANSI('6', '#'): DECDWL: 2*width */
4eeb7d09 2069 nlattr = LATTR_WIDE;
2070 break;
2071 }
887035a5 2072 ldata = lineptr(term->curs.y);
2073 ldata[term->cols] &= ~LATTR_MODE;
2074 ldata[term->cols] |= nlattr;
4eeb7d09 2075 }
2076 break;
a263e4f6 2077 /* GZD4: G0 designate 94-set */
4eeb7d09 2078 case ANSI('A', '('):
2079 compatibility(VT100);
64734920 2080 if (!term->cfg.no_remote_charset)
887035a5 2081 term->cset_attr[0] = ATTR_GBCHR;
4eeb7d09 2082 break;
2083 case ANSI('B', '('):
2084 compatibility(VT100);
64734920 2085 if (!term->cfg.no_remote_charset)
887035a5 2086 term->cset_attr[0] = ATTR_ASCII;
4eeb7d09 2087 break;
2088 case ANSI('0', '('):
2089 compatibility(VT100);
64734920 2090 if (!term->cfg.no_remote_charset)
887035a5 2091 term->cset_attr[0] = ATTR_LINEDRW;
4eeb7d09 2092 break;
d3cb5465 2093 case ANSI('U', '('):
2094 compatibility(OTHER);
64734920 2095 if (!term->cfg.no_remote_charset)
887035a5 2096 term->cset_attr[0] = ATTR_SCOACS;
d3cb5465 2097 break;
a263e4f6 2098 /* G1D4: G1-designate 94-set */
4eeb7d09 2099 case ANSI('A', ')'):
2100 compatibility(VT100);
64734920 2101 if (!term->cfg.no_remote_charset)
887035a5 2102 term->cset_attr[1] = ATTR_GBCHR;
4eeb7d09 2103 break;
2104 case ANSI('B', ')'):
2105 compatibility(VT100);
64734920 2106 if (!term->cfg.no_remote_charset)
887035a5 2107 term->cset_attr[1] = ATTR_ASCII;
4eeb7d09 2108 break;
2109 case ANSI('0', ')'):
2110 compatibility(VT100);
64734920 2111 if (!term->cfg.no_remote_charset)
887035a5 2112 term->cset_attr[1] = ATTR_LINEDRW;
4eeb7d09 2113 break;
d3cb5465 2114 case ANSI('U', ')'):
2115 compatibility(OTHER);
64734920 2116 if (!term->cfg.no_remote_charset)
887035a5 2117 term->cset_attr[1] = ATTR_SCOACS;
d3cb5465 2118 break;
a263e4f6 2119 /* DOCS: Designate other coding system */
4eeb7d09 2120 case ANSI('8', '%'): /* Old Linux code */
2121 case ANSI('G', '%'):
2122 compatibility(OTHER);
64734920 2123 if (!term->cfg.no_remote_charset)
887035a5 2124 term->utf = 1;
4eeb7d09 2125 break;
2126 case ANSI('@', '%'):
2127 compatibility(OTHER);
64734920 2128 if (!term->cfg.no_remote_charset)
887035a5 2129 term->utf = 0;
4eeb7d09 2130 break;
374330e2 2131 }
2132 break;
32874aea 2133 case SEEN_CSI:
887035a5 2134 term->termstate = TOPLEVEL; /* default */
32874aea 2135 if (isdigit(c)) {
887035a5 2136 if (term->esc_nargs <= ARGS_MAX) {
2137 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2138 term->esc_args[term->esc_nargs - 1] = 0;
2139 term->esc_args[term->esc_nargs - 1] =
2140 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
32874aea 2141 }
887035a5 2142 term->termstate = SEEN_CSI;
32874aea 2143 } else if (c == ';') {
887035a5 2144 if (++term->esc_nargs <= ARGS_MAX)
2145 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2146 term->termstate = SEEN_CSI;
32874aea 2147 } else if (c < '@') {
887035a5 2148 if (term->esc_query)
2149 term->esc_query = -1;
32874aea 2150 else if (c == '?')
887035a5 2151 term->esc_query = TRUE;
32874aea 2152 else
887035a5 2153 term->esc_query = c;
2154 term->termstate = SEEN_CSI;
32874aea 2155 } else
887035a5 2156 switch (ANSI(c, term->esc_query)) {
a263e4f6 2157 case 'A': /* CUU: move up N lines */
887035a5 2158 move(term, term->curs.x,
2159 term->curs.y - def(term->esc_args[0], 1), 1);
2160 term->seen_disp_event = TRUE;
32874aea 2161 break;
a263e4f6 2162 case 'e': /* VPR: move down N lines */
32874aea 2163 compatibility(ANSI);
5442e92f 2164 /* FALLTHROUGH */
a263e4f6 2165 case 'B': /* CUD: Cursor down */
887035a5 2166 move(term, term->curs.x,
2167 term->curs.y + def(term->esc_args[0], 1), 1);
2168 term->seen_disp_event = TRUE;
32874aea 2169 break;
a263e4f6 2170 case ANSI('c', '>'): /* DA: report xterm version */
32874aea 2171 compatibility(OTHER);
2172 /* this reports xterm version 136 so that VIM can
2173 use the drag messages from the mouse reporting */
b9d7bcad 2174 if (term->ldisc)
2175 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
32874aea 2176 break;
a263e4f6 2177 case 'a': /* HPR: move right N cols */
5442e92f 2178 compatibility(ANSI);
2179 /* FALLTHROUGH */
a263e4f6 2180 case 'C': /* CUF: Cursor right */
887035a5 2181 move(term, term->curs.x + def(term->esc_args[0], 1),
2182 term->curs.y, 1);
2183 term->seen_disp_event = TRUE;
32874aea 2184 break;
a263e4f6 2185 case 'D': /* CUB: move left N cols */
887035a5 2186 move(term, term->curs.x - def(term->esc_args[0], 1),
2187 term->curs.y, 1);
2188 term->seen_disp_event = TRUE;
32874aea 2189 break;
a263e4f6 2190 case 'E': /* CNL: move down N lines and CR */
32874aea 2191 compatibility(ANSI);
887035a5 2192 move(term, 0,
2193 term->curs.y + def(term->esc_args[0], 1), 1);
2194 term->seen_disp_event = TRUE;
32874aea 2195 break;
a263e4f6 2196 case 'F': /* CPL: move up N lines and CR */
32874aea 2197 compatibility(ANSI);
887035a5 2198 move(term, 0,
2199 term->curs.y - def(term->esc_args[0], 1), 1);
2200 term->seen_disp_event = TRUE;
32874aea 2201 break;
a263e4f6 2202 case 'G': /* CHA */
2203 case '`': /* HPA: set horizontal posn */
32874aea 2204 compatibility(ANSI);
887035a5 2205 move(term, def(term->esc_args[0], 1) - 1,
2206 term->curs.y, 0);
2207 term->seen_disp_event = TRUE;
32874aea 2208 break;
a263e4f6 2209 case 'd': /* VPA: set vertical posn */
32874aea 2210 compatibility(ANSI);
887035a5 2211 move(term, term->curs.x,
2212 ((term->dec_om ? term->marg_t : 0) +
2213 def(term->esc_args[0], 1) - 1),
2214 (term->dec_om ? 2 : 0));
2215 term->seen_disp_event = TRUE;
32874aea 2216 break;
a263e4f6 2217 case 'H': /* CUP */
2218 case 'f': /* HVP: set horz and vert posns at once */
887035a5 2219 if (term->esc_nargs < 2)
2220 term->esc_args[1] = ARG_DEFAULT;
2221 move(term, def(term->esc_args[1], 1) - 1,
2222 ((term->dec_om ? term->marg_t : 0) +
2223 def(term->esc_args[0], 1) - 1),
2224 (term->dec_om ? 2 : 0));
2225 term->seen_disp_event = TRUE;
32874aea 2226 break;
a263e4f6 2227 case 'J': /* ED: erase screen or parts of it */
32874aea 2228 {
887035a5 2229 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 2230 if (i > 3)
2231 i = 0;
887035a5 2232 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
32874aea 2233 }
887035a5 2234 term->disptop = 0;
2235 term->seen_disp_event = TRUE;
32874aea 2236 break;
a263e4f6 2237 case 'K': /* EL: erase line or parts of it */
32874aea 2238 {
887035a5 2239 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 2240 if (i > 3)
2241 i = 0;
887035a5 2242 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
32874aea 2243 }
887035a5 2244 term->seen_disp_event = TRUE;
32874aea 2245 break;
a263e4f6 2246 case 'L': /* IL: insert lines */
32874aea 2247 compatibility(VT102);
887035a5 2248 if (term->curs.y <= term->marg_b)
2249 scroll(term, term->curs.y, term->marg_b,
2250 -def(term->esc_args[0], 1), FALSE);
32874aea 2251 fix_cpos;
887035a5 2252 term->seen_disp_event = TRUE;
32874aea 2253 break;
a263e4f6 2254 case 'M': /* DL: delete lines */
32874aea 2255 compatibility(VT102);
887035a5 2256 if (term->curs.y <= term->marg_b)
2257 scroll(term, term->curs.y, term->marg_b,
2258 def(term->esc_args[0], 1),
32874aea 2259 TRUE);
2260 fix_cpos;
887035a5 2261 term->seen_disp_event = TRUE;
32874aea 2262 break;
a263e4f6 2263 case '@': /* ICH: insert chars */
32874aea 2264 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2265 compatibility(VT102);
887035a5 2266 insch(term, def(term->esc_args[0], 1));
2267 term->seen_disp_event = TRUE;
32874aea 2268 break;
a263e4f6 2269 case 'P': /* DCH: delete chars */
32874aea 2270 compatibility(VT102);
887035a5 2271 insch(term, -def(term->esc_args[0], 1));
2272 term->seen_disp_event = TRUE;
32874aea 2273 break;
a263e4f6 2274 case 'c': /* DA: terminal type query */
32874aea 2275 compatibility(VT100);
2276 /* This is the response for a VT102 */
b9d7bcad 2277 if (term->ldisc)
2278 ldisc_send(term->ldisc, term->id_string,
2279 strlen(term->id_string), 0);
32874aea 2280 break;
a263e4f6 2281 case 'n': /* DSR: cursor position query */
b9d7bcad 2282 if (term->ldisc) {
2283 if (term->esc_args[0] == 6) {
2284 char buf[32];
2285 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2286 term->curs.x + 1);
2287 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2288 } else if (term->esc_args[0] == 5) {
2289 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2290 }
32874aea 2291 }
2292 break;
a263e4f6 2293 case 'h': /* SM: toggle modes to high */
32874aea 2294 case ANSI_QUE('h'):
2295 compatibility(VT100);
2296 {
2297 int i;
887035a5 2298 for (i = 0; i < term->esc_nargs; i++)
2299 toggle_mode(term, term->esc_args[i],
2300 term->esc_query, TRUE);
32874aea 2301 }
2302 break;
a263e4f6 2303 case 'i': /* MC: Media copy */
b44b307a 2304 case ANSI_QUE('i'):
2305 compatibility(VT100);
2306 {
887035a5 2307 if (term->esc_nargs != 1) break;
64734920 2308 if (term->esc_args[0] == 5 && *term->cfg.printer) {
887035a5 2309 term->printing = TRUE;
2310 term->only_printing = !term->esc_query;
2311 term->print_state = 0;
2312 term_print_setup(term);
2313 } else if (term->esc_args[0] == 4 &&
2314 term->printing) {
2315 term_print_finish(term);
b44b307a 2316 }
2317 }
2318 break;
a263e4f6 2319 case 'l': /* RM: toggle modes to low */
32874aea 2320 case ANSI_QUE('l'):
2321 compatibility(VT100);
2322 {
2323 int i;
887035a5 2324 for (i = 0; i < term->esc_nargs; i++)
2325 toggle_mode(term, term->esc_args[i],
2326 term->esc_query, FALSE);
32874aea 2327 }
2328 break;
a263e4f6 2329 case 'g': /* TBC: clear tabs */
32874aea 2330 compatibility(VT100);
887035a5 2331 if (term->esc_nargs == 1) {
2332 if (term->esc_args[0] == 0) {
2333 term->tabs[term->curs.x] = FALSE;
2334 } else if (term->esc_args[0] == 3) {
32874aea 2335 int i;
887035a5 2336 for (i = 0; i < term->cols; i++)
2337 term->tabs[i] = FALSE;
32874aea 2338 }
2339 }
2340 break;
a263e4f6 2341 case 'r': /* DECSTBM: set scroll margins */
32874aea 2342 compatibility(VT100);
887035a5 2343 if (term->esc_nargs <= 2) {
32874aea 2344 int top, bot;
887035a5 2345 top = def(term->esc_args[0], 1) - 1;
2346 bot = (term->esc_nargs <= 1
2347 || term->esc_args[1] == 0 ?
2348 term->rows :
2349 def(term->esc_args[1], term->rows)) - 1;
2350 if (bot >= term->rows)
2351 bot = term->rows - 1;
32874aea 2352 /* VTTEST Bug 9 - if region is less than 2 lines
2353 * don't change region.
2354 */
2355 if (bot - top > 0) {
887035a5 2356 term->marg_t = top;
2357 term->marg_b = bot;
2358 term->curs.x = 0;
32874aea 2359 /*
2360 * I used to think the cursor should be
2361 * placed at the top of the newly marginned
2362 * area. Apparently not: VMS TPU falls over
2363 * if so.
2364 *
887035a5 2365 * Well actually it should for
2366 * Origin mode - RDB
32874aea 2367 */
887035a5 2368 term->curs.y = (term->dec_om ?
2369 term->marg_t : 0);
32874aea 2370 fix_cpos;
887035a5 2371 term->seen_disp_event = TRUE;
32874aea 2372 }
2373 }
2374 break;
a263e4f6 2375 case 'm': /* SGR: set graphics rendition */
32874aea 2376 {
2377 /*
887035a5 2378 * A VT100 without the AVO only had one
2379 * attribute, either underline or
2380 * reverse video depending on the
2381 * cursor type, this was selected by
2382 * CSI 7m.
32874aea 2383 *
2384 * case 2:
887035a5 2385 * This is sometimes DIM, eg on the
2386 * GIGI and Linux
32874aea 2387 * case 8:
4eeb7d09 2388 * This is sometimes INVIS various ANSI.
32874aea 2389 * case 21:
2390 * This like 22 disables BOLD, DIM and INVIS
2391 *
887035a5 2392 * The ANSI colours appear on any
2393 * terminal that has colour (obviously)
2394 * but the interaction between sgr0 and
2395 * the colours varies but is usually
2396 * related to the background colour
2397 * erase item. The interaction between
2398 * colour attributes and the mono ones
2399 * is also very implementation
2400 * dependent.
32874aea 2401 *
887035a5 2402 * The 39 and 49 attributes are likely
2403 * to be unimplemented.
32874aea 2404 */
2405 int i;
887035a5 2406 for (i = 0; i < term->esc_nargs; i++) {
2407 switch (def(term->esc_args[i], 0)) {
32874aea 2408 case 0: /* restore defaults */
69761fd5 2409 term->curr_attr = term->default_attr;
32874aea 2410 break;
2411 case 1: /* enable bold */
2412 compatibility(VT100AVO);
887035a5 2413 term->curr_attr |= ATTR_BOLD;
32874aea 2414 break;
2415 case 21: /* (enable double underline) */
2416 compatibility(OTHER);
2417 case 4: /* enable underline */
2418 compatibility(VT100AVO);
887035a5 2419 term->curr_attr |= ATTR_UNDER;
32874aea 2420 break;
2421 case 5: /* enable blink */
2422 compatibility(VT100AVO);
887035a5 2423 term->curr_attr |= ATTR_BLINK;
32874aea 2424 break;
1b20b972 2425 case 6: /* SCO light bkgrd */
2426 compatibility(SCOANSI);
2427 term->blink_is_real = FALSE;
2428 term->curr_attr |= ATTR_BLINK;
2429 break;
32874aea 2430 case 7: /* enable reverse video */
887035a5 2431 term->curr_attr |= ATTR_REVERSE;
32874aea 2432 break;
d3cb5465 2433 case 10: /* SCO acs off */
2434 compatibility(SCOANSI);
64734920 2435 if (term->cfg.no_remote_charset) break;
887035a5 2436 term->sco_acs = 0; break;
d3cb5465 2437 case 11: /* SCO acs on */
2438 compatibility(SCOANSI);
64734920 2439 if (term->cfg.no_remote_charset) break;
887035a5 2440 term->sco_acs = 1; break;
5dc6132d 2441 case 12: /* SCO acs on, |0x80 */
d3cb5465 2442 compatibility(SCOANSI);
64734920 2443 if (term->cfg.no_remote_charset) break;
887035a5 2444 term->sco_acs = 2; break;
32874aea 2445 case 22: /* disable bold */
2446 compatibility2(OTHER, VT220);
887035a5 2447 term->curr_attr &= ~ATTR_BOLD;
32874aea 2448 break;
2449 case 24: /* disable underline */
2450 compatibility2(OTHER, VT220);
887035a5 2451 term->curr_attr &= ~ATTR_UNDER;
32874aea 2452 break;
2453 case 25: /* disable blink */
2454 compatibility2(OTHER, VT220);
887035a5 2455 term->curr_attr &= ~ATTR_BLINK;
32874aea 2456 break;
2457 case 27: /* disable reverse video */
2458 compatibility2(OTHER, VT220);
887035a5 2459 term->curr_attr &= ~ATTR_REVERSE;
32874aea 2460 break;
2461 case 30:
2462 case 31:
2463 case 32:
2464 case 33:
2465 case 34:
2466 case 35:
2467 case 36:
2468 case 37:
2469 /* foreground */
887035a5 2470 term->curr_attr &= ~ATTR_FGMASK;
2471 term->curr_attr |=
2472 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
32874aea 2473 break;
37ca32ed 2474 case 90:
2475 case 91:
2476 case 92:
2477 case 93:
2478 case 94:
2479 case 95:
2480 case 96:
2481 case 97:
2482 /* xterm-style bright foreground */
2483 term->curr_attr &= ~ATTR_FGMASK;
2484 term->curr_attr |=
2485 ((term->esc_args[i] - 90 + 16)
2486 << ATTR_FGSHIFT);
2487 break;
32874aea 2488 case 39: /* default-foreground */
887035a5 2489 term->curr_attr &= ~ATTR_FGMASK;
2490 term->curr_attr |= ATTR_DEFFG;
32874aea 2491 break;
2492 case 40:
2493 case 41:
2494 case 42:
2495 case 43:
2496 case 44:
2497 case 45:
2498 case 46:
2499 case 47:
2500 /* background */
887035a5 2501 term->curr_attr &= ~ATTR_BGMASK;
2502 term->curr_attr |=
2503 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
32874aea 2504 break;
37ca32ed 2505 case 100:
2506 case 101:
2507 case 102:
2508 case 103:
2509 case 104:
2510 case 105:
2511 case 106:
2512 case 107:
2513 /* xterm-style bright background */
2514 term->curr_attr &= ~ATTR_BGMASK;
2515 term->curr_attr |=
2516 ((term->esc_args[i] - 100 + 16)
2517 << ATTR_BGSHIFT);
2518 break;
32874aea 2519 case 49: /* default-background */
887035a5 2520 term->curr_attr &= ~ATTR_BGMASK;
2521 term->curr_attr |= ATTR_DEFBG;
32874aea 2522 break;
2523 }
2524 }
887035a5 2525 if (term->use_bce)
2526 term->erase_char = (' ' | ATTR_ASCII |
2527 (term->curr_attr &
2528 (ATTR_FGMASK |
2529 ATTR_BGMASK)));
32874aea 2530 }
2531 break;
2532 case 's': /* save cursor */
887035a5 2533 save_cursor(term, TRUE);
32874aea 2534 break;
2535 case 'u': /* restore cursor */
887035a5 2536 save_cursor(term, FALSE);
2537 term->seen_disp_event = TRUE;
32874aea 2538 break;
a263e4f6 2539 case 't': /* DECSLPP: set page size - ie window height */
374330e2 2540 /*
32874aea 2541 * VT340/VT420 sequence DECSLPP, DEC only allows values
2542 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2543 * illegal values (eg first arg 1..9) for window changing
2544 * and reports.
2545 */
887035a5 2546 if (term->esc_nargs <= 1
2547 && (term->esc_args[0] < 1 ||
2548 term->esc_args[0] >= 24)) {
68f9b3d9 2549 compatibility(VT340TEXT);
64734920 2550 if (!term->cfg.no_remote_resize)
a8327734 2551 request_resize(term->frontend, term->cols,
887035a5 2552 def(term->esc_args[0], 24));
2553 deselect(term);
2554 } else if (term->esc_nargs >= 1 &&
2555 term->esc_args[0] >= 1 &&
2556 term->esc_args[0] < 24) {
68f9b3d9 2557 compatibility(OTHER);
2558
887035a5 2559 switch (term->esc_args[0]) {
68f9b3d9 2560 int x, y, len;
2561 char buf[80], *p;
2562 case 1:
a8327734 2563 set_iconic(term->frontend, FALSE);
68f9b3d9 2564 break;
2565 case 2:
a8327734 2566 set_iconic(term->frontend, TRUE);
68f9b3d9 2567 break;
2568 case 3:
887035a5 2569 if (term->esc_nargs >= 3) {
64734920 2570 if (!term->cfg.no_remote_resize)
a8327734 2571 move_window(term->frontend,
2572 def(term->esc_args[1], 0),
887035a5 2573 def(term->esc_args[2], 0));
68f9b3d9 2574 }
2575 break;
2576 case 4:
2577 /* We should resize the window to a given
2578 * size in pixels here, but currently our
2579 * resizing code isn't healthy enough to
2580 * manage it. */
2581 break;
2582 case 5:
a8327734 2583 /* move to top */
2584 set_zorder(term->frontend, TRUE);
68f9b3d9 2585 break;
2586 case 6:
a8327734 2587 /* move to bottom */
2588 set_zorder(term->frontend, FALSE);
68f9b3d9 2589 break;
2590 case 7:
a8327734 2591 refresh_window(term->frontend);
68f9b3d9 2592 break;
2593 case 8:
887035a5 2594 if (term->esc_nargs >= 3) {
64734920 2595 if (!term->cfg.no_remote_resize)
a8327734 2596 request_resize(term->frontend,
64734920 2597 def(term->esc_args[2], term->cfg.width),
2598 def(term->esc_args[1], term->cfg.height));
68f9b3d9 2599 }
2600 break;
2601 case 9:
887035a5 2602 if (term->esc_nargs >= 2)
a8327734 2603 set_zoomed(term->frontend,
2604 term->esc_args[1] ?
887035a5 2605 TRUE : FALSE);
68f9b3d9 2606 break;
2607 case 11:
b9d7bcad 2608 if (term->ldisc)
2609 ldisc_send(term->ldisc,
a8327734 2610 is_iconic(term->frontend) ?
2611 "\033[1t" : "\033[2t", 4, 0);
68f9b3d9 2612 break;
2613 case 13:
b9d7bcad 2614 if (term->ldisc) {
a8327734 2615 get_window_pos(term->frontend, &x, &y);
b9d7bcad 2616 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2617 ldisc_send(term->ldisc, buf, len, 0);
2618 }
68f9b3d9 2619 break;
2620 case 14:
b9d7bcad 2621 if (term->ldisc) {
a8327734 2622 get_window_pixels(term->frontend, &x, &y);
b9d7bcad 2623 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2624 ldisc_send(term->ldisc, buf, len, 0);
2625 }
68f9b3d9 2626 break;
2627 case 18:
b9d7bcad 2628 if (term->ldisc) {
2629 len = sprintf(buf, "\033[8;%d;%dt",
2630 term->rows, term->cols);
2631 ldisc_send(term->ldisc, buf, len, 0);
2632 }
68f9b3d9 2633 break;
2634 case 19:
2635 /*
2636 * Hmmm. Strictly speaking we
2637 * should return `the size of the
2638 * screen in characters', but
2639 * that's not easy: (a) window
2640 * furniture being what it is it's
2641 * hard to compute, and (b) in
2642 * resize-font mode maximising the
2643 * window wouldn't change the
2644 * number of characters. *shrug*. I
2645 * think we'll ignore it for the
2646 * moment and see if anyone
2647 * complains, and then ask them
2648 * what they would like it to do.
2649 */
2650 break;
2651 case 20:
7fcdebd3 2652 if (term->ldisc &&
2653 !term->cfg.no_remote_qtitle) {
a8327734 2654 p = get_window_title(term->frontend, TRUE);
b9d7bcad 2655 len = strlen(p);
2656 ldisc_send(term->ldisc, "\033]L", 3, 0);
2657 ldisc_send(term->ldisc, p, len, 0);
2658 ldisc_send(term->ldisc, "\033\\", 2, 0);
2659 }
68f9b3d9 2660 break;
2661 case 21:
7fcdebd3 2662 if (term->ldisc &&
2663 !term->cfg.no_remote_qtitle) {
a8327734 2664 p = get_window_title(term->frontend,FALSE);
b9d7bcad 2665 len = strlen(p);
2666 ldisc_send(term->ldisc, "\033]l", 3, 0);
2667 ldisc_send(term->ldisc, p, len, 0);
2668 ldisc_send(term->ldisc, "\033\\", 2, 0);
2669 }
68f9b3d9 2670 break;
2671 }
32874aea 2672 }
2673 break;
a263e4f6 2674 case 'S': /* SU: Scroll up */
1a837633 2675 compatibility(SCOANSI);
887035a5 2676 scroll(term, term->marg_t, term->marg_b,
2677 def(term->esc_args[0], 1), TRUE);
1a837633 2678 fix_cpos;
887035a5 2679 term->wrapnext = FALSE;
2680 term->seen_disp_event = TRUE;
1a837633 2681 break;
a263e4f6 2682 case 'T': /* SD: Scroll down */
1a837633 2683 compatibility(SCOANSI);
887035a5 2684 scroll(term, term->marg_t, term->marg_b,
2685 -def(term->esc_args[0], 1), TRUE);
1a837633 2686 fix_cpos;
887035a5 2687 term->wrapnext = FALSE;
2688 term->seen_disp_event = TRUE;
1a837633 2689 break;
a263e4f6 2690 case ANSI('|', '*'): /* DECSNLS */
2691 /*
32874aea 2692 * Set number of lines on screen
a263e4f6 2693 * VT420 uses VGA like hardware and can
2694 * support any size in reasonable range
2695 * (24..49 AIUI) with no default specified.
32874aea 2696 */
2697 compatibility(VT420);
887035a5 2698 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
64734920 2699 if (!term->cfg.no_remote_resize)
a8327734 2700 request_resize(term->frontend, term->cols,
887035a5 2701 def(term->esc_args[0],
64734920 2702 term->cfg.height));
887035a5 2703 deselect(term);
32874aea 2704 }
2705 break;
a263e4f6 2706 case ANSI('|', '$'): /* DECSCPP */
2707 /*
32874aea 2708 * Set number of columns per page
a263e4f6 2709 * Docs imply range is only 80 or 132, but
2710 * I'll allow any.
32874aea 2711 */
2712 compatibility(VT340TEXT);
887035a5 2713 if (term->esc_nargs <= 1) {
64734920 2714 if (!term->cfg.no_remote_resize)
a8327734 2715 request_resize(term->frontend,
2716 def(term->esc_args[0],
64734920 2717 term->cfg.width), term->rows);
887035a5 2718 deselect(term);
32874aea 2719 }
2720 break;
a263e4f6 2721 case 'X': /* ECH: write N spaces w/o moving cursor */
2722 /* XXX VTTEST says this is vt220, vt510 manual
2723 * says vt100 */
32874aea 2724 compatibility(ANSIMIN);
2725 {
887035a5 2726 int n = def(term->esc_args[0], 1);
32874aea 2727 pos cursplus;
887035a5 2728 unsigned long *p = term->cpos;
2729 if (n > term->cols - term->curs.x)
2730 n = term->cols - term->curs.x;
2731 cursplus = term->curs;
32874aea 2732 cursplus.x += n;
3c7366f8 2733 check_boundary(term, term->curs.x, term->curs.y);
2734 check_boundary(term, term->curs.x+n, term->curs.y);
887035a5 2735 check_selection(term, term->curs, cursplus);
32874aea 2736 while (n--)
887035a5 2737 *p++ = term->erase_char;
2738 term->seen_disp_event = TRUE;
32874aea 2739 }
2740 break;
a263e4f6 2741 case 'x': /* DECREQTPARM: report terminal characteristics */
32874aea 2742 compatibility(VT100);
b9d7bcad 2743 if (term->ldisc) {
32874aea 2744 char buf[32];
887035a5 2745 int i = def(term->esc_args[0], 0);
32874aea 2746 if (i == 0 || i == 1) {
2747 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2748 buf[2] += i;
b9d7bcad 2749 ldisc_send(term->ldisc, buf, 20, 0);
32874aea 2750 }
2751 }
2752 break;
a263e4f6 2753 case 'Z': /* CBT: BackTab for xterm */
979f6987 2754 compatibility(OTHER);
2755 {
887035a5 2756 int i = def(term->esc_args[0], 1);
2757 pos old_curs = term->curs;
979f6987 2758
887035a5 2759 for(;i>0 && term->curs.x>0; i--) {
979f6987 2760 do {
887035a5 2761 term->curs.x--;
2762 } while (term->curs.x >0 &&
2763 !term->tabs[term->curs.x]);
979f6987 2764 }
2765 fix_cpos;
887035a5 2766 check_selection(term, old_curs, term->curs);
979f6987 2767 }
2768 break;
1b20b972 2769 case ANSI('c', '='): /* Hide or Show Cursor */
2770 compatibility(SCOANSI);
2771 switch(term->esc_args[0]) {
2772 case 0: /* hide cursor */
2773 term->cursor_on = FALSE;
2774 break;
2775 case 1: /* restore cursor */
2776 term->big_cursor = FALSE;
2777 term->cursor_on = TRUE;
2778 break;
2779 case 2: /* block cursor */
2780 term->big_cursor = TRUE;
2781 term->cursor_on = TRUE;
2782 break;
2783 }
2784 break;
2785 case ANSI('C', '='):
2786 /*
2787 * set cursor start on scanline esc_args[0] and
2788 * end on scanline esc_args[1].If you set
2789 * the bottom scan line to a value less than
2790 * the top scan line, the cursor will disappear.
2791 */
2792 compatibility(SCOANSI);
2793 if (term->esc_nargs >= 2) {
2794 if (term->esc_args[0] > term->esc_args[1])
2795 term->cursor_on = FALSE;
2796 else
2797 term->cursor_on = TRUE;
2798 }
2799 break;
2800 case ANSI('D', '='):
2801 compatibility(SCOANSI);
2802 term->blink_is_real = FALSE;
2803 if (term->esc_args[0]>=1)
2804 term->curr_attr |= ATTR_BLINK;
2805 else
2806 term->curr_attr &= ~ATTR_BLINK;
2807 break;
2808 case ANSI('E', '='):
2809 compatibility(SCOANSI);
2810 term->blink_is_real = (term->esc_args[0] >= 1);
2811 break;
2812 case ANSI('F', '='): /* set normal foreground */
2813 compatibility(SCOANSI);
2814 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 2815 long colour =
2816 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 2817 ((term->esc_args[0] & 0x8) << 1)) <<
2818 ATTR_FGSHIFT;
69761fd5 2819 term->curr_attr &= ~ATTR_FGMASK;
2820 term->curr_attr |= colour;
2821 term->default_attr &= ~ATTR_FGMASK;
2822 term->default_attr |= colour;
1b20b972 2823 }
2824 break;
2825 case ANSI('G', '='): /* set normal background */
2826 compatibility(SCOANSI);
2827 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 2828 long colour =
2829 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 2830 ((term->esc_args[0] & 0x8) << 1)) <<
2831 ATTR_BGSHIFT;
69761fd5 2832 term->curr_attr &= ~ATTR_BGMASK;
2833 term->curr_attr |= colour;
2834 term->default_attr &= ~ATTR_BGMASK;
2835 term->default_attr |= colour;
1b20b972 2836 }
2837 break;
32874aea 2838 case ANSI('L', '='):
1b20b972 2839 compatibility(SCOANSI);
887035a5 2840 term->use_bce = (term->esc_args[0] <= 0);
2841 term->erase_char = ERASE_CHAR;
2842 if (term->use_bce)
2843 term->erase_char = (' ' | ATTR_ASCII |
2844 (term->curr_attr &
2845 (ATTR_FGMASK | ATTR_BGMASK)));
32874aea 2846 break;
a263e4f6 2847 case ANSI('p', '"'): /* DECSCL: set compat level */
887035a5 2848 /*
2849 * Allow the host to make this emulator a
2850 * 'perfect' VT102. This first appeared in
2851 * the VT220, but we do need to get back to
2852 * PuTTY mode so I won't check it.
e14a5a13 2853 *
4eeb7d09 2854 * The arg in 40..42,50 are a PuTTY extension.
32874aea 2855 * The 2nd arg, 8bit vs 7bit is not checked.
2856 *
887035a5 2857 * Setting VT102 mode should also change
2858 * the Fkeys to generate PF* codes as a
2859 * real VT102 has no Fkeys. The VT220 does
2860 * this, F11..F13 become ESC,BS,LF other
2861 * Fkeys send nothing.
32874aea 2862 *
2863 * Note ESC c will NOT change this!
374330e2 2864 */
32874aea 2865
887035a5 2866 switch (term->esc_args[0]) {
32874aea 2867 case 61:
887035a5 2868 term->compatibility_level &= ~TM_VTXXX;
2869 term->compatibility_level |= TM_VT102;
374330e2 2870 break;
32874aea 2871 case 62:
887035a5 2872 term->compatibility_level &= ~TM_VTXXX;
2873 term->compatibility_level |= TM_VT220;
32874aea 2874 break;
2875
2876 default:
887035a5 2877 if (term->esc_args[0] > 60 &&
2878 term->esc_args[0] < 70)
2879 term->compatibility_level |= TM_VTXXX;
32874aea 2880 break;
2881
2882 case 40:
887035a5 2883 term->compatibility_level &= TM_VTXXX;
374330e2 2884 break;
32874aea 2885 case 41:
887035a5 2886 term->compatibility_level = TM_PUTTY;
374330e2 2887 break;
32874aea 2888 case 42:
887035a5 2889 term->compatibility_level = TM_SCOANSI;
374330e2 2890 break;
32874aea 2891
2892 case ARG_DEFAULT:
887035a5 2893 term->compatibility_level = TM_PUTTY;
32874aea 2894 break;
2895 case 50:
2896 break;
2897 }
2898
2899 /* Change the response to CSI c */
887035a5 2900 if (term->esc_args[0] == 50) {
32874aea 2901 int i;
2902 char lbuf[64];
887035a5 2903 strcpy(term->id_string, "\033[?");
2904 for (i = 1; i < term->esc_nargs; i++) {
32874aea 2905 if (i != 1)
887035a5 2906 strcat(term->id_string, ";");
2907 sprintf(lbuf, "%d", term->esc_args[i]);
2908 strcat(term->id_string, lbuf);
32874aea 2909 }
887035a5 2910 strcat(term->id_string, "c");
32874aea 2911 }
2912#if 0
2913 /* Is this a good idea ?
2914 * Well we should do a soft reset at this point ...
2915 */
2916 if (!has_compat(VT420) && has_compat(VT100)) {
64734920 2917 if (!term->cfg.no_remote_resize) {
887035a5 2918 if (term->reset_132)
0d2086c5 2919 request_resize(132, 24);
2920 else
2921 request_resize(80, 24);
2922 }
374330e2 2923 }
32874aea 2924#endif
2925 break;
374330e2 2926 }
374330e2 2927 break;
32874aea 2928 case SEEN_OSC:
887035a5 2929 term->osc_w = FALSE;
32874aea 2930 switch (c) {
2931 case 'P': /* Linux palette sequence */
887035a5 2932 term->termstate = SEEN_OSC_P;
2933 term->osc_strlen = 0;
32874aea 2934 break;
2935 case 'R': /* Linux palette reset */
a8327734 2936 palette_reset(term->frontend);
887035a5 2937 term_invalidate(term);
2938 term->termstate = TOPLEVEL;
32874aea 2939 break;
2940 case 'W': /* word-set */
887035a5 2941 term->termstate = SEEN_OSC_W;
2942 term->osc_w = TRUE;
32874aea 2943 break;
2944 case '0':
2945 case '1':
2946 case '2':
2947 case '3':
2948 case '4':
2949 case '5':
2950 case '6':
2951 case '7':
2952 case '8':
2953 case '9':
887035a5 2954 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 2955 break;
2956 case 'L':
2957 /*
2958 * Grotty hack to support xterm and DECterm title
2959 * sequences concurrently.
2960 */
887035a5 2961 if (term->esc_args[0] == 2) {
2962 term->esc_args[0] = 1;
32874aea 2963 break;
2964 }
2965 /* else fall through */
2966 default:
887035a5 2967 term->termstate = OSC_STRING;
2968 term->osc_strlen = 0;
e14a5a13 2969 }
2970 break;
32874aea 2971 case OSC_STRING:
2972 /*
2973 * This OSC stuff is EVIL. It takes just one character to get into
2974 * sysline mode and it's not initially obvious how to get out.
2975 * So I've added CR and LF as string aborts.
2976 * This shouldn't effect compatibility as I believe embedded
2977 * control characters are supposed to be interpreted (maybe?)
2978 * and they don't display anything useful anyway.
2979 *
2980 * -- RDB
e14a5a13 2981 */
92e23ad9 2982 if (c == '\012' || c == '\015') {
887035a5 2983 term->termstate = TOPLEVEL;
32874aea 2984 } else if (c == 0234 || c == '\007') {
2985 /*
2986 * These characters terminate the string; ST and BEL
2987 * terminate the sequence and trigger instant
2988 * processing of it, whereas ESC goes back to SEEN_ESC
2989 * mode unless it is followed by \, in which case it is
2990 * synonymous with ST in the first place.
2991 */
887035a5 2992 do_osc(term);
2993 term->termstate = TOPLEVEL;
32874aea 2994 } else if (c == '\033')
887035a5 2995 term->termstate = OSC_MAYBE_ST;
2996 else if (term->osc_strlen < OSC_STR_MAX)
2997 term->osc_string[term->osc_strlen++] = c;
374330e2 2998 break;
32874aea 2999 case SEEN_OSC_P:
374330e2 3000 {
887035a5 3001 int max = (term->osc_strlen == 0 ? 21 : 16);
32874aea 3002 int val;
3003 if (c >= '0' && c <= '9')
3004 val = c - '0';
3005 else if (c >= 'A' && c <= 'A' + max - 10)
3006 val = c - 'A' + 10;
3007 else if (c >= 'a' && c <= 'a' + max - 10)
3008 val = c - 'a' + 10;
2d466ffd 3009 else {
887035a5 3010 term->termstate = TOPLEVEL;
2d466ffd 3011 break;
3012 }
887035a5 3013 term->osc_string[term->osc_strlen++] = val;
3014 if (term->osc_strlen >= 7) {
a8327734 3015 palette_set(term->frontend, term->osc_string[0],
887035a5 3016 term->osc_string[1] * 16 + term->osc_string[2],
3017 term->osc_string[3] * 16 + term->osc_string[4],
3018 term->osc_string[5] * 16 + term->osc_string[6]);
3019 term_invalidate(term);
3020 term->termstate = TOPLEVEL;
374330e2 3021 }
3022 }
3023 break;
32874aea 3024 case SEEN_OSC_W:
3025 switch (c) {
3026 case '0':
3027 case '1':
3028 case '2':
3029 case '3':
3030 case '4':
3031 case '5':
3032 case '6':
3033 case '7':
3034 case '8':
3035 case '9':
887035a5 3036 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 3037 break;
3038 default:
887035a5 3039 term->termstate = OSC_STRING;
3040 term->osc_strlen = 0;
ec55b220 3041 }
32874aea 3042 break;
32874aea 3043 case VT52_ESC:
887035a5 3044 term->termstate = TOPLEVEL;
3045 term->seen_disp_event = TRUE;
c9def1b8 3046 switch (c) {
32874aea 3047 case 'A':
887035a5 3048 move(term, term->curs.x, term->curs.y - 1, 1);
32874aea 3049 break;
3050 case 'B':
887035a5 3051 move(term, term->curs.x, term->curs.y + 1, 1);
32874aea 3052 break;
3053 case 'C':
887035a5 3054 move(term, term->curs.x + 1, term->curs.y, 1);
32874aea 3055 break;
3056 case 'D':
887035a5 3057 move(term, term->curs.x - 1, term->curs.y, 1);
32874aea 3058 break;
4eeb7d09 3059 /*
3060 * From the VT100 Manual
3061 * NOTE: The special graphics characters in the VT100
3062 * are different from those in the VT52
3063 *
3064 * From VT102 manual:
3065 * 137 _ Blank - Same
3066 * 140 ` Reserved - Humm.
3067 * 141 a Solid rectangle - Similar
3068 * 142 b 1/ - Top half of fraction for the
3069 * 143 c 3/ - subscript numbers below.
3070 * 144 d 5/
3071 * 145 e 7/
3072 * 146 f Degrees - Same
3073 * 147 g Plus or minus - Same
3074 * 150 h Right arrow
3075 * 151 i Ellipsis (dots)
3076 * 152 j Divide by
3077 * 153 k Down arrow
3078 * 154 l Bar at scan 0
3079 * 155 m Bar at scan 1
3080 * 156 n Bar at scan 2
3081 * 157 o Bar at scan 3 - Similar
3082 * 160 p Bar at scan 4 - Similar
3083 * 161 q Bar at scan 5 - Similar
3084 * 162 r Bar at scan 6 - Same
3085 * 163 s Bar at scan 7 - Similar
3086 * 164 t Subscript 0
3087 * 165 u Subscript 1
3088 * 166 v Subscript 2
3089 * 167 w Subscript 3
3090 * 170 x Subscript 4
3091 * 171 y Subscript 5
3092 * 172 z Subscript 6
3093 * 173 { Subscript 7
3094 * 174 | Subscript 8
3095 * 175 } Subscript 9
3096 * 176 ~ Paragraph
3097 *
3098 */
32874aea 3099 case 'F':
887035a5 3100 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
32874aea 3101 break;
3102 case 'G':
887035a5 3103 term->cset_attr[term->cset = 0] = ATTR_ASCII;
32874aea 3104 break;
3105 case 'H':
887035a5 3106 move(term, 0, 0, 0);
32874aea 3107 break;
3108 case 'I':
887035a5 3109 if (term->curs.y == 0)
3110 scroll(term, 0, term->rows - 1, -1, TRUE);
3111 else if (term->curs.y > 0)
3112 term->curs.y--;
32874aea 3113 fix_cpos;
887035a5 3114 term->wrapnext = FALSE;
32874aea 3115 break;
3116 case 'J':
887035a5 3117 erase_lots(term, FALSE, FALSE, TRUE);
3118 term->disptop = 0;
c9def1b8 3119 break;
32874aea 3120 case 'K':
887035a5 3121 erase_lots(term, TRUE, FALSE, TRUE);
32874aea 3122 break;
4eeb7d09 3123#if 0
32874aea 3124 case 'V':
3125 /* XXX Print cursor line */
3126 break;
3127 case 'W':
3128 /* XXX Start controller mode */
3129 break;
3130 case 'X':
3131 /* XXX Stop controller mode */
3132 break;
4eeb7d09 3133#endif
32874aea 3134 case 'Y':
887035a5 3135 term->termstate = VT52_Y1;
32874aea 3136 break;
3137 case 'Z':
b9d7bcad 3138 if (term->ldisc)
3139 ldisc_send(term->ldisc, "\033/Z", 3, 0);
32874aea 3140 break;
3141 case '=':
887035a5 3142 term->app_keypad_keys = TRUE;
32874aea 3143 break;
3144 case '>':
887035a5 3145 term->app_keypad_keys = FALSE;
32874aea 3146 break;
3147 case '<':
3148 /* XXX This should switch to VT100 mode not current or default
3149 * VT mode. But this will only have effect in a VT220+
3150 * emulation.
3151 */
887035a5 3152 term->vt52_mode = FALSE;
64734920 3153 term->blink_is_real = term->cfg.blinktext;
32874aea 3154 break;
4eeb7d09 3155#if 0
32874aea 3156 case '^':
3157 /* XXX Enter auto print mode */
3158 break;
3159 case '_':
3160 /* XXX Exit auto print mode */
3161 break;
3162 case ']':
3163 /* XXX Print screen */
3164 break;
4eeb7d09 3165#endif
3166
3167#ifdef VT52_PLUS
3168 case 'E':
3169 /* compatibility(ATARI) */
887035a5 3170 move(term, 0, 0, 0);
3171 erase_lots(term, FALSE, FALSE, TRUE);
3172 term->disptop = 0;
4eeb7d09 3173 break;
3174 case 'L':
3175 /* compatibility(ATARI) */
887035a5 3176 if (term->curs.y <= term->marg_b)
3177 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4eeb7d09 3178 break;
3179 case 'M':
3180 /* compatibility(ATARI) */
887035a5 3181 if (term->curs.y <= term->marg_b)
3182 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4eeb7d09 3183 break;
3184 case 'b':
3185 /* compatibility(ATARI) */
887035a5 3186 term->termstate = VT52_FG;
4eeb7d09 3187 break;
3188 case 'c':
3189 /* compatibility(ATARI) */
887035a5 3190 term->termstate = VT52_BG;
4eeb7d09 3191 break;
3192 case 'd':
3193 /* compatibility(ATARI) */
887035a5 3194 erase_lots(term, FALSE, TRUE, FALSE);
3195 term->disptop = 0;
4eeb7d09 3196 break;
3197 case 'e':
3198 /* compatibility(ATARI) */
887035a5 3199 term->cursor_on = TRUE;
4eeb7d09 3200 break;
3201 case 'f':
3202 /* compatibility(ATARI) */
887035a5 3203 term->cursor_on = FALSE;
4eeb7d09 3204 break;
3205 /* case 'j': Save cursor position - broken on ST */
3206 /* case 'k': Restore cursor position */
3207 case 'l':
3208 /* compatibility(ATARI) */
887035a5 3209 erase_lots(term, TRUE, TRUE, TRUE);
3210 term->curs.x = 0;
3211 term->wrapnext = FALSE;
4eeb7d09 3212 fix_cpos;
3213 break;
3214 case 'o':
3215 /* compatibility(ATARI) */
887035a5 3216 erase_lots(term, TRUE, TRUE, FALSE);
4eeb7d09 3217 break;
3218 case 'p':
3219 /* compatibility(ATARI) */
887035a5 3220 term->curr_attr |= ATTR_REVERSE;
4eeb7d09 3221 break;
3222 case 'q':
3223 /* compatibility(ATARI) */
887035a5 3224 term->curr_attr &= ~ATTR_REVERSE;
4eeb7d09 3225 break;
3226 case 'v': /* wrap Autowrap on - Wyse style */
3227 /* compatibility(ATARI) */
887035a5 3228 term->wrap = 1;
4eeb7d09 3229 break;
3230 case 'w': /* Autowrap off */
3231 /* compatibility(ATARI) */
887035a5 3232 term->wrap = 0;
4eeb7d09 3233 break;
3234
3235 case 'R':
3236 /* compatibility(OTHER) */
887035a5 3237 term->vt52_bold = FALSE;
3238 term->curr_attr = ATTR_DEFAULT;
3239 if (term->use_bce)
3240 term->erase_char = (' ' | ATTR_ASCII |
3241 (term->curr_attr &
3242 (ATTR_FGMASK | ATTR_BGMASK)));
4eeb7d09 3243 break;
3244 case 'S':
3245 /* compatibility(VI50) */
887035a5 3246 term->curr_attr |= ATTR_UNDER;
4eeb7d09 3247 break;
3248 case 'W':
3249 /* compatibility(VI50) */
887035a5 3250 term->curr_attr &= ~ATTR_UNDER;
4eeb7d09 3251 break;
3252 case 'U':
3253 /* compatibility(VI50) */
887035a5 3254 term->vt52_bold = TRUE;
3255 term->curr_attr |= ATTR_BOLD;
4eeb7d09 3256 break;
3257 case 'T':
3258 /* compatibility(VI50) */
887035a5 3259 term->vt52_bold = FALSE;
3260 term->curr_attr &= ~ATTR_BOLD;
4eeb7d09 3261 break;
3262#endif
c9def1b8 3263 }
e14a5a13 3264 break;
32874aea 3265 case VT52_Y1:
887035a5 3266 term->termstate = VT52_Y2;
3267 move(term, term->curs.x, c - ' ', 0);
e14a5a13 3268 break;
32874aea 3269 case VT52_Y2:
887035a5 3270 term->termstate = TOPLEVEL;
3271 move(term, c - ' ', term->curs.y, 0);
e14a5a13 3272 break;
4eeb7d09 3273
3274#ifdef VT52_PLUS
3275 case VT52_FG:
887035a5 3276 term->termstate = TOPLEVEL;
3277 term->curr_attr &= ~ATTR_FGMASK;
3278 term->curr_attr &= ~ATTR_BOLD;
3279 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3280 if ((c & 0x8) || term->vt52_bold)
3281 term->curr_attr |= ATTR_BOLD;
3282
3283 if (term->use_bce)
3284 term->erase_char = (' ' | ATTR_ASCII |
3285 (term->curr_attr &
3286 (ATTR_FGMASK | ATTR_BGMASK)));
4eeb7d09 3287 break;
3288 case VT52_BG:
887035a5 3289 term->termstate = TOPLEVEL;
3290 term->curr_attr &= ~ATTR_BGMASK;
3291 term->curr_attr &= ~ATTR_BLINK;
3292 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
4eeb7d09 3293
3294 /* Note: bold background */
3295 if (c & 0x8)
887035a5 3296 term->curr_attr |= ATTR_BLINK;
4eeb7d09 3297
887035a5 3298 if (term->use_bce)
3299 term->erase_char = (' ' | ATTR_ASCII |
3300 (term->curr_attr &
3301 (ATTR_FGMASK | ATTR_BGMASK)));
4eeb7d09 3302 break;
3303#endif
2d466ffd 3304 default: break; /* placate gcc warning about enum use */
e14a5a13 3305 }
887035a5 3306 if (term->selstate != NO_SELECTION) {
3307 pos cursplus = term->curs;
4facdf84 3308 incpos(cursplus);
887035a5 3309 check_selection(term, term->curs, cursplus);
4facdf84 3310 }
374330e2 3311 }
b44b307a 3312
887035a5 3313 term_print_flush(term);
11cc5e30 3314 logflush(term->logctx);
374330e2 3315}
3316
4eeb7d09 3317#if 0
374330e2 3318/*
3319 * Compare two lines to determine whether they are sufficiently
3320 * alike to scroll-optimise one to the other. Return the degree of
3321 * similarity.
3322 */
887035a5 3323static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
32874aea 3324{
374330e2 3325 int i, n;
3326
887035a5 3327 for (i = n = 0; i < term->cols; i++)
374330e2 3328 n += (*a++ == *b++);
3329 return n;
3330}
4eeb7d09 3331#endif
374330e2 3332
3333/*
f0fccd51 3334 * To prevent having to run the reasonably tricky bidi algorithm
3335 * too many times, we maintain a cache of the last lineful of data
3336 * fed to the algorithm on each line of the display.
3337 */
3338static int term_bidi_cache_hit(Terminal *term, int line,
3339 unsigned long *lbefore, int width)
3340{
3341 if (!term->pre_bidi_cache)
3342 return FALSE; /* cache doesn't even exist yet! */
3343
3344 if (line >= term->bidi_cache_size)
3345 return FALSE; /* cache doesn't have this many lines */
3346
3347 if (!term->pre_bidi_cache[line])
3348 return FALSE; /* cache doesn't contain _this_ line */
3349
3350 if (!memcmp(term->pre_bidi_cache[line], lbefore,
3351 width * sizeof(unsigned long)))
3352 return TRUE; /* aha! the line matches the cache */
3353
3354 return FALSE; /* it didn't match. */
3355}
3356
3357static void term_bidi_cache_store(Terminal *term, int line,
3358 unsigned long *lbefore,
3359 unsigned long *lafter, int width)
3360{
3361 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
3362 int j = term->bidi_cache_size;
3363 term->bidi_cache_size = line+1;
3364 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
3365 term->bidi_cache_size,
3366 unsigned long *);
3367 term->post_bidi_cache = sresize(term->post_bidi_cache,
3368 term->bidi_cache_size,
3369 unsigned long *);
3370 while (j < term->bidi_cache_size) {
3371 term->pre_bidi_cache[j] = term->post_bidi_cache[j] = NULL;
3372 j++;
3373 }
3374 }
3375
3376 sfree(term->pre_bidi_cache[line]);
3377 sfree(term->post_bidi_cache[line]);
3378
3379 term->pre_bidi_cache[line] = snewn(width, unsigned long);
3380 term->post_bidi_cache[line] = snewn(width, unsigned long);
3381
3382 memcpy(term->pre_bidi_cache[line], lbefore, width * sizeof(unsigned long));
3383 memcpy(term->post_bidi_cache[line], lafter, width * sizeof(unsigned long));
3384}
3385
3386/*
374330e2 3387 * Given a context, update the window. Out of paranoia, we don't
3388 * allow WM_PAINT responses to do scrolling optimisations.
3389 */
887035a5 3390static void do_paint(Terminal *term, Context ctx, int may_optimise)
32874aea 3391{
f0fccd51 3392 int i, it, j, our_curs_y, our_curs_x;
4eeb7d09 3393 unsigned long rv, cursor;
4facdf84 3394 pos scrpos;
374330e2 3395 char ch[1024];
4eeb7d09 3396 long cursor_background = ERASE_CHAR;
286c6b86 3397 unsigned long ticks;
341eb978 3398#ifdef OPTIMISE_SCROLL
3399 struct scrollregion *sr;
3400#endif /* OPTIMISE_SCROLL */
374330e2 3401
156686ef 3402 /*
3403 * Check the visual bell state.
3404 */
887035a5 3405 if (term->in_vbell) {
f7f27309 3406 ticks = GETTICKCOUNT();
887035a5 3407 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
f0fccd51 3408 term->in_vbell = FALSE;
3409 }
156686ef 3410
887035a5 3411 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4eeb7d09 3412
156686ef 3413 /* Depends on:
3414 * screen array, disptop, scrtop,
3415 * selection, rv,
3416 * cfg.blinkpc, blink_is_real, tblinker,
4eeb7d09 3417 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
156686ef 3418 */
4eeb7d09 3419
3420 /* Has the cursor position or type changed ? */
887035a5 3421 if (term->cursor_on) {
3422 if (term->has_focus) {
64734920 3423 if (term->blinker || !term->cfg.blink_cur)
4eeb7d09 3424 cursor = TATTR_ACTCURS;
32874aea 3425 else
3426 cursor = 0;
3427 } else
4eeb7d09 3428 cursor = TATTR_PASCURS;
887035a5 3429 if (term->wrapnext)
4eeb7d09 3430 cursor |= TATTR_RIGHTCURS;
32874aea 3431 } else
156686ef 3432 cursor = 0;
887035a5 3433 our_curs_y = term->curs.y - term->disptop;
3c7366f8 3434 {
3435 /*
3436 * Adjust the cursor position in the case where it's
3437 * resting on the right-hand half of a CJK wide character.
3438 * xterm's behaviour here, which seems adequate to me, is
3439 * to display the cursor covering the _whole_ character,
3440 * exactly as if it were one space to the left.
3441 */
3442 unsigned long *ldata = lineptr(term->curs.y);
3443 our_curs_x = term->curs.x;
3444 if (our_curs_x > 0 &&
3445 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3446 our_curs_x--;
3447 }
887035a5 3448
3449 if (term->dispcurs && (term->curstype != cursor ||
3450 term->dispcurs !=
3451 term->disptext + our_curs_y * (term->cols + 1) +
3c7366f8 3452 our_curs_x)) {
887035a5 3453 if (term->dispcurs > term->disptext &&
3c7366f8 3454 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
887035a5 3455 term->dispcurs[-1] |= ATTR_INVALID;
3456 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3457 term->dispcurs[1] |= ATTR_INVALID;
3458 *term->dispcurs |= ATTR_INVALID;
3459 term->curstype = 0;
4eeb7d09 3460 }
887035a5 3461 term->dispcurs = NULL;
4eeb7d09 3462
341eb978 3463#ifdef OPTIMISE_SCROLL
3464 /* Do scrolls */
3465 sr = term->scrollhead;
3466 while (sr) {
3467 struct scrollregion *next = sr->next;
3468 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3469 sfree(sr);
3470 sr = next;
3471 }
3472 term->scrollhead = term->scrolltail = NULL;
3473#endif /* OPTIMISE_SCROLL */
3474
4eeb7d09 3475 /* The normal screen data */
887035a5 3476 for (i = 0; i < term->rows; i++) {
4facdf84 3477 unsigned long *ldata;
3478 int lattr;
6908fed7 3479 int idx, dirty_line, dirty_run, selected;
4eeb7d09 3480 unsigned long attr = 0;
3481 int updated_line = 0;
3482 int start = 0;
3483 int ccount = 0;
3484 int last_run_dirty = 0;
3485
887035a5 3486 scrpos.y = i + term->disptop;
4facdf84 3487 ldata = lineptr(scrpos.y);
887035a5 3488 lattr = (ldata[term->cols] & LATTR_MODE);
4eeb7d09 3489
887035a5 3490 idx = i * (term->cols + 1);
3491 dirty_run = dirty_line = (ldata[term->cols] !=
3492 term->disptext[idx + term->cols]);
3493 term->disptext[idx + term->cols] = ldata[term->cols];
4eeb7d09 3494
f0fccd51 3495 /* Do Arabic shaping and bidi. */
3496 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
3497
3498 if (!term_bidi_cache_hit(term, i, ldata, term->cols)) {
3499
3500 for(it=0; it<term->cols ; it++)
3501 {
3502 int uc = (ldata[it] & 0xFFFF);
3503
3504 switch (uc & CSET_MASK) {
3505 case ATTR_LINEDRW:
3506 if (!term->cfg.rawcnp) {
3507 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3508 break;
3509 }
3510 case ATTR_ASCII:
3511 uc = term->ucsdata->unitab_line[uc & 0xFF];
3512 break;
3513 case ATTR_SCOACS:
3514 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3515 break;
3516 }
3517 switch (uc & CSET_MASK) {
3518 case ATTR_ACP:
3519 uc = term->ucsdata->unitab_font[uc & 0xFF];
3520 break;
3521 case ATTR_OEMCP:
3522 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3523 break;
3524 }
3525
3526 term->wcFrom[it].origwc = term->wcFrom[it].wc = uc;
3527 term->wcFrom[it].index = it;
3528 }
3529
3530 if(!term->cfg.bidi)
3531 do_bidi(term->wcFrom, term->cols);
3532
3533 /* this is saved iff done from inside the shaping */
3534 if(!term->cfg.bidi && term->cfg.arabicshaping)
3535 for(it=0; it<term->cols; it++)
3536 term->wcTo[it] = term->wcFrom[it];
3537
3538 if(!term->cfg.arabicshaping)
3539 do_shape(term->wcFrom, term->wcTo, term->cols);
3540
3541 for(it=0; it<term->cols ; it++)
3542 {
3543 term->ltemp[it] = ldata[term->wcTo[it].index];
3544
3545 if (term->wcTo[it].origwc != term->wcTo[it].wc)
3546 term->ltemp[it] = ((term->ltemp[it] & 0xFFFF0000) |
3547 term->wcTo[it].wc);
3548 }
3549 term_bidi_cache_store(term, i, ldata, term->ltemp, term->cols);
3550 ldata = term->ltemp;
3551 } else {
3552 ldata = term->post_bidi_cache[i];
3553 }
3554 }
3555
887035a5 3556 for (j = 0; j < term->cols; j++, idx++) {
4eeb7d09 3557 unsigned long tattr, tchar;
3558 unsigned long *d = ldata + j;
3559 int break_run;
4facdf84 3560 scrpos.x = j;
32874aea 3561
4eeb7d09 3562 tchar = (*d & (CHAR_MASK | CSET_MASK));
3563 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3564 switch (tchar & CSET_MASK) {
3565 case ATTR_ASCII:
21d2b241 3566 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4eeb7d09 3567 break;
3568 case ATTR_LINEDRW:
21d2b241 3569 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4eeb7d09 3570 break;
d3cb5465 3571 case ATTR_SCOACS:
21d2b241 3572 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
d3cb5465 3573 break;
4eeb7d09 3574 }
3575 tattr |= (tchar & CSET_MASK);
3576 tchar &= CHAR_MASK;
552b7a5c 3577 if (j < term->cols-1 &&
3578 (d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3579 tattr |= ATTR_WIDE;
4eeb7d09 3580
3581 /* Video reversing things */
f278d6f8 3582 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3583 if (term->seltype == LEXICOGRAPHIC)
3584 selected = (posle(term->selstart, scrpos) &&
3585 poslt(scrpos, term->selend));
3586 else
3587 selected = (posPle(term->selstart, scrpos) &&
3588 posPlt(scrpos, term->selend));
3589 } else
3590 selected = FALSE;
4eeb7d09 3591 tattr = (tattr ^ rv
6908fed7 3592 ^ (selected ? ATTR_REVERSE : 0));
4eeb7d09 3593
3594 /* 'Real' blinking ? */
887035a5 3595 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3596 if (term->has_focus && term->tblinker) {
fc6f0fc2 3597 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
c9def1b8 3598 }
4eeb7d09 3599 tattr &= ~ATTR_BLINK;
c9def1b8 3600 }
374330e2 3601
5a73255e 3602 /*
3603 * Check the font we'll _probably_ be using to see if
3604 * the character is wide when we don't want it to be.
3605 */
887035a5 3606 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
5a73255e 3607 if ((tattr & ATTR_WIDE) == 0 &&
2102eb8a 3608 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
5a73255e 3609 tattr |= ATTR_NARROW;
887035a5 3610 } else if (term->disptext[idx]&ATTR_NARROW)
5a73255e 3611 tattr |= ATTR_NARROW;
3612
4eeb7d09 3613 /* Cursor here ? Save the 'background' */
3c7366f8 3614 if (i == our_curs_y && j == our_curs_x) {
4eeb7d09 3615 cursor_background = tattr | tchar;
887035a5 3616 term->dispcurs = term->disptext + idx;
4eeb7d09 3617 }
374330e2 3618
887035a5 3619 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
4eeb7d09 3620 dirty_line = TRUE;
3621
2647b103 3622 break_run = (((tattr ^ attr) & term->attr_mask) ||
3623 j - start >= sizeof(ch));
4eeb7d09 3624
3625 /* Special hack for VT100 Linedraw glyphs */
3626 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3627 && tchar <= 0xBD) break_run = TRUE;
3628
21d2b241 3629 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
887035a5 3630 if ((tchar | tattr) == term->disptext[idx])
4eeb7d09 3631 break_run = TRUE;
3632 else if (!dirty_run && ccount == 1)
3633 break_run = TRUE;
374330e2 3634 }
4eeb7d09 3635
3636 if (break_run) {
3637 if ((dirty_run || last_run_dirty) && ccount > 0) {
3638 do_text(ctx, start, i, ch, ccount, attr, lattr);
3639 updated_line = 1;
3640 }
3641 start = j;
3642 ccount = 0;
3643 attr = tattr;
21d2b241 3644 if (term->ucsdata->dbcs_screenfont)
4eeb7d09 3645 last_run_dirty = dirty_run;
3646 dirty_run = dirty_line;
3647 }
3648
887035a5 3649 if ((tchar | tattr) != term->disptext[idx])
4eeb7d09 3650 dirty_run = TRUE;
3651 ch[ccount++] = (char) tchar;
887035a5 3652 term->disptext[idx] = tchar | tattr;
4eeb7d09 3653
3654 /* If it's a wide char step along to the next one. */
3655 if (tattr & ATTR_WIDE) {
887035a5 3656 if (++j < term->cols) {
4eeb7d09 3657 idx++;
3658 d++;
3c7366f8 3659 /*
3660 * By construction above, the cursor should not
3661 * be on the right-hand half of this character.
3662 * Ever.
3663 */
3664 assert(!(i == our_curs_y && j == our_curs_x));
887035a5 3665 if (term->disptext[idx] != *d)
4eeb7d09 3666 dirty_run = TRUE;
887035a5 3667 term->disptext[idx] = *d;
374330e2 3668 }
374330e2 3669 }
4eeb7d09 3670 }
3671 if (dirty_run && ccount > 0) {
3672 do_text(ctx, start, i, ch, ccount, attr, lattr);
3673 updated_line = 1;
3674 }
3675
3676 /* Cursor on this line ? (and changed) */
887035a5 3677 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
4eeb7d09 3678 ch[0] = (char) (cursor_background & CHAR_MASK);
3679 attr = (cursor_background & ATTR_MASK) | cursor;
3c7366f8 3680 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
887035a5 3681 term->curstype = cursor;
374330e2 3682 }
3683 }
3684}
3685
3686/*
e14a5a13 3687 * Flick the switch that says if blinking things should be shown or hidden.
3688 */
3689
887035a5 3690void term_blink(Terminal *term, int flg)
32874aea 3691{
e14a5a13 3692 long now, blink_diff;
3693
f7f27309 3694 now = GETTICKCOUNT();
887035a5 3695 blink_diff = now - term->last_tblink;
c9def1b8 3696
1d3989ed 3697 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3698 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
887035a5 3699 term->last_tblink = now;
3700 term->tblinker = !term->tblinker;
c9def1b8 3701 }
3702
e14a5a13 3703 if (flg) {
887035a5 3704 term->blinker = 1;
3705 term->last_blink = now;
e14a5a13 3706 return;
32874aea 3707 }
e14a5a13 3708
887035a5 3709 blink_diff = now - term->last_blink;
e14a5a13 3710
f7f27309 3711 /* Make sure the cursor blinks no faster than system blink rate */
3712 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
32874aea 3713 return;
3714
887035a5 3715 term->last_blink = now;
3716 term->blinker = !term->blinker;
e14a5a13 3717}
3718
3719/*
374330e2 3720 * Invalidate the whole screen so it will be repainted in full.
3721 */
887035a5 3722void term_invalidate(Terminal *term)
32874aea 3723{
374330e2 3724 int i;
3725
887035a5 3726 for (i = 0; i < term->rows * (term->cols + 1); i++)
3727 term->disptext[i] = ATTR_INVALID;
374330e2 3728}
3729
3730/*
3731 * Paint the window in response to a WM_PAINT message.
3732 */
887035a5 3733void term_paint(Terminal *term, Context ctx,
c1b55581 3734 int left, int top, int right, int bottom, int immediately)
32874aea 3735{
5a73255e 3736 int i, j;
3737 if (left < 0) left = 0;
3738 if (top < 0) top = 0;
887035a5 3739 if (right >= term->cols) right = term->cols-1;
3740 if (bottom >= term->rows) bottom = term->rows-1;
3741
3742 for (i = top; i <= bottom && i < term->rows; i++) {
3743 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3744 LATTR_MODE) == LATTR_NORM)
3745 for (j = left; j <= right && j < term->cols; j++)
3746 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
c9def1b8 3747 else
887035a5 3748 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3749 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
c9def1b8 3750 }
374330e2 3751
e14a5a13 3752 /* This should happen soon enough, also for some reason it sometimes
3753 * fails to actually do anything when re-sizing ... painting the wrong
3754 * window perhaps ?
32874aea 3755 */
c1b55581 3756 if (immediately)
887035a5 3757 do_paint (term, ctx, FALSE);
374330e2 3758}
3759
3760/*
3761 * Attempt to scroll the scrollback. The second parameter gives the
3762 * position we want to scroll to; the first is +1 to denote that
3763 * this position is relative to the beginning of the scrollback, -1
3764 * to denote it is relative to the end, and 0 to denote that it is
3765 * relative to the current position.
3766 */
887035a5 3767void term_scroll(Terminal *term, int rel, int where)
32874aea 3768{
876e5d5e 3769 int sbtop = -sblines(term);
37d2a505 3770#ifdef OPTIMISE_SCROLL
3771 int olddisptop = term->disptop;
3772 int shift;
3773#endif /* OPTIMISE_SCROLL */
887035a5 3774
3775 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3776 if (term->disptop < sbtop)
3777 term->disptop = sbtop;
3778 if (term->disptop > 0)
3779 term->disptop = 0;
3780 update_sbar(term);
37d2a505 3781#ifdef OPTIMISE_SCROLL
3782 shift = (term->disptop - olddisptop);
3783 if (shift < term->rows && shift > -term->rows)
3784 scroll_display(term, 0, term->rows - 1, shift);
3785#endif /* OPTIMISE_SCROLL */
887035a5 3786 term_update(term);
374330e2 3787}
3788
c076966d 3789static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
32874aea 3790{
4eeb7d09 3791 wchar_t *workbuf;
3792 wchar_t *wbptr; /* where next char goes within workbuf */
6908fed7 3793 int old_top_x;
32874aea 3794 int wblen = 0; /* workbuf len */
3795 int buflen; /* amount of memory allocated to workbuf */
bc1235d4 3796
4eeb7d09 3797 buflen = 5120; /* Default size */
3d88e64d 3798 workbuf = snewn(buflen, wchar_t);
4eeb7d09 3799 wbptr = workbuf; /* start filling here */
6908fed7 3800 old_top_x = top.x; /* needed for rect==1 */
bc1235d4 3801
4facdf84 3802 while (poslt(top, bottom)) {
bc1235d4 3803 int nl = FALSE;
4facdf84 3804 unsigned long *ldata = lineptr(top.y);
260f3dec 3805 pos nlpos;
4facdf84 3806
6908fed7 3807 /*
3808 * nlpos will point at the maximum position on this line we
3809 * should copy up to. So we start it at the end of the
3810 * line...
3811 */
4facdf84 3812 nlpos.y = top.y;
887035a5 3813 nlpos.x = term->cols;
bc1235d4 3814
6908fed7 3815 /*
3816 * ... move it backwards if there's unused space at the end
3817 * of the line (and also set `nl' if this is the case,
3818 * because in normal selection mode this means we need a
3819 * newline at the end)...
3820 */
887035a5 3821 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
4eeb7d09 3822 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3823 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3824 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3825 && poslt(top, nlpos))
3826 decpos(nlpos);
4facdf84 3827 if (poslt(nlpos, bottom))
bc1235d4 3828 nl = TRUE;
3c7366f8 3829 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3830 /* Ignore the last char on the line in a WRAPPED2 line. */
3831 decpos(nlpos);
bc1235d4 3832 }
6908fed7 3833
3834 /*
3835 * ... and then clip it to the terminal x coordinate if
3836 * we're doing rectangular selection. (In this case we
3837 * still did the above, so that copying e.g. the right-hand
3838 * column from a table doesn't fill with spaces on the
3839 * right.)
3840 */
3841 if (rect) {
3842 if (nlpos.x > bottom.x)
3843 nlpos.x = bottom.x;
3844 nl = (top.y < bottom.y);
3845 }
3846
4facdf84 3847 while (poslt(top, bottom) && poslt(top, nlpos)) {
4eeb7d09 3848#if 0
3849 char cbuf[16], *p;
3850 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3851#else
3852 wchar_t cbuf[16], *p;
3853 int uc = (ldata[top.x] & 0xFFFF);
3854 int set, c;
3855
3856 if (uc == UCSWIDE) {
3857 top.x++;
3858 continue;
3859 }
3860
3861 switch (uc & CSET_MASK) {
3862 case ATTR_LINEDRW:
64734920 3863 if (!term->cfg.rawcnp) {
21d2b241 3864 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4eeb7d09 3865 break;
32874aea 3866 }
4eeb7d09 3867 case ATTR_ASCII:
21d2b241 3868 uc = term->ucsdata->unitab_line[uc & 0xFF];
4eeb7d09 3869 break;
d3cb5465 3870 case ATTR_SCOACS:
21d2b241 3871 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
d3cb5465 3872 break;
d3a22f79 3873 }
4eeb7d09 3874 switch (uc & CSET_MASK) {
3875 case ATTR_ACP:
21d2b241 3876 uc = term->ucsdata->unitab_font[uc & 0xFF];
4eeb7d09 3877 break;
3878 case ATTR_OEMCP:
21d2b241 3879 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 3880 break;
3881 }
3882
3883 set = (uc & CSET_MASK);
3884 c = (uc & CHAR_MASK);
3885 cbuf[0] = uc;
3886 cbuf[1] = 0;
3887
3888 if (DIRECT_FONT(uc)) {
3889 if (c >= ' ' && c != 0x7F) {
e63ae276 3890 char buf[4];
4eeb7d09 3891 WCHAR wbuf[4];
3892 int rv;
21d2b241 3893 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4eeb7d09 3894 buf[0] = c;
7b966c9b 3895 buf[1] = (char) (0xFF & ldata[top.x + 1]);
21d2b241 3896 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4eeb7d09 3897 top.x++;
3898 } else {
3899 buf[0] = c;
21d2b241 3900 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4eeb7d09 3901 }
bc1235d4 3902
4eeb7d09 3903 if (rv > 0) {
3904 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3905 cbuf[rv] = 0;
d3a22f79 3906 }
d3a22f79 3907 }
4eeb7d09 3908 }
3909#endif
3910
3911 for (p = cbuf; *p; p++) {
3912 /* Enough overhead for trailing NL and nul */
3913 if (wblen >= buflen - 16) {
3d88e64d 3914 buflen += 100;
3915 workbuf = sresize(workbuf, buflen, wchar_t);
4eeb7d09 3916 wbptr = workbuf + wblen;
3917 }
3918 wblen++;
3919 *wbptr++ = *p;
bc1235d4 3920 }
4facdf84 3921 top.x++;
bc1235d4 3922 }
3923 if (nl) {
3924 int i;
4eeb7d09 3925 for (i = 0; i < sel_nl_sz; i++) {
32874aea 3926 wblen++;
bc1235d4 3927 *wbptr++ = sel_nl[i];
3928 }
3929 }
4facdf84 3930 top.y++;
6908fed7 3931 top.x = rect ? old_top_x : 0;
bc1235d4 3932 }
e6346999 3933#if SELECTION_NUL_TERMINATED
4eeb7d09 3934 wblen++;
3935 *wbptr++ = 0;
e6346999 3936#endif
c076966d 3937 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
32874aea 3938 if (buflen > 0) /* indicates we allocated this buffer */
bc1235d4 3939 sfree(workbuf);
bc1235d4 3940}
4eeb7d09 3941
887035a5 3942void term_copyall(Terminal *term)
32874aea 3943{
4facdf84 3944 pos top;
40132dba 3945 pos bottom;
3946 tree234 *screen = term->screen;
876e5d5e 3947 top.y = -sblines(term);
4facdf84 3948 top.x = 0;
40132dba 3949 bottom.y = find_last_nonempty_line(term, screen);
3950 bottom.x = term->cols;
3951 clipme(term, top, bottom, 0, TRUE);
4eeb7d09 3952}
3953
3954/*
887035a5 3955 * The wordness array is mainly for deciding the disposition of the
3956 * US-ASCII characters.
4eeb7d09 3957 */
887035a5 3958static int wordtype(Terminal *term, int uc)
4eeb7d09 3959{
b9d7bcad 3960 struct ucsword {
4eeb7d09 3961 int start, end, ctype;
b9d7bcad 3962 };
3963 static const struct ucsword ucs_words[] = {
4eeb7d09 3964 {
3965 128, 160, 0}, {
3966 161, 191, 1}, {
3967 215, 215, 1}, {
3968 247, 247, 1}, {
3969 0x037e, 0x037e, 1}, /* Greek question mark */
3970 {
3971 0x0387, 0x0387, 1}, /* Greek ano teleia */
3972 {
3973 0x055a, 0x055f, 1}, /* Armenian punctuation */
3974 {
3975 0x0589, 0x0589, 1}, /* Armenian full stop */
3976 {
3977 0x0700, 0x070d, 1}, /* Syriac punctuation */
3978 {
3979 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3980 {
3981 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3982 {
3983 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3984 {
3985 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3986 {
3987 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3988 {
3989 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3990 {
3991 0x2000, 0x200a, 0}, /* Various spaces */
3992 {
3993 0x2070, 0x207f, 2}, /* superscript */
3994 {
3995 0x2080, 0x208f, 2}, /* subscript */
3996 {
3997 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3998 {
3999 0x3000, 0x3000, 0}, /* ideographic space */
4000 {
4001 0x3001, 0x3020, 1}, /* ideographic punctuation */
4002 {
4003 0x303f, 0x309f, 3}, /* Hiragana */
4004 {
4005 0x30a0, 0x30ff, 3}, /* Katakana */
4006 {
4007 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4008 {
4009 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4010 {
4011 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4012 {
4013 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4014 {
4015 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4016 {
4017 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4018 {
4019 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4020 {
4021 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4022 {
4023 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4024 {
4025 0, 0, 0}
4026 };
b9d7bcad 4027 const struct ucsword *wptr;
4eeb7d09 4028
4029 uc &= (CSET_MASK | CHAR_MASK);
4030
4031 switch (uc & CSET_MASK) {
4032 case ATTR_LINEDRW:
21d2b241 4033 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4eeb7d09 4034 break;
4035 case ATTR_ASCII:
21d2b241 4036 uc = term->ucsdata->unitab_line[uc & 0xFF];
4eeb7d09 4037 break;
d3cb5465 4038 case ATTR_SCOACS:
21d2b241 4039 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
d3cb5465 4040 break;
4eeb7d09 4041 }
4042 switch (uc & CSET_MASK) {
4043 case ATTR_ACP:
21d2b241 4044 uc = term->ucsdata->unitab_font[uc & 0xFF];
4eeb7d09 4045 break;
4046 case ATTR_OEMCP:
21d2b241 4047 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 4048 break;
4049 }
4050
5a73255e 4051 /* For DBCS font's I can't do anything usefull. Even this will sometimes
4052 * fail as there's such a thing as a double width space. :-(
4053 */
21d2b241 4054 if (term->ucsdata->dbcs_screenfont &&
4055 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5a73255e 4056 return (uc != ' ');
4057
4eeb7d09 4058 if (uc < 0x80)
887035a5 4059 return term->wordness[uc];
4eeb7d09 4060
4061 for (wptr = ucs_words; wptr->start; wptr++) {
4062 if (uc >= wptr->start && uc <= wptr->end)
4063 return wptr->ctype;
4064 }
4065
4066 return 2;
bc1235d4 4067}
4068
374330e2 4069/*
4070 * Spread the selection outwards according to the selection mode.
4071 */
887035a5 4072static pos sel_spread_half(Terminal *term, pos p, int dir)
32874aea 4073{
4facdf84 4074 unsigned long *ldata;
374330e2 4075 short wvalue;
876e5d5e 4076 int topy = -sblines(term);
374330e2 4077
4facdf84 4078 ldata = lineptr(p.y);
374330e2 4079
887035a5 4080 switch (term->selmode) {
374330e2 4081 case SM_CHAR:
4082 /*
4083 * In this mode, every character is a separate unit, except
4084 * for runs of spaces at the end of a non-wrapping line.
4085 */
887035a5 4086 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
4087 unsigned long *q = ldata + term->cols;
4facdf84 4088 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
374330e2 4089 q--;
887035a5 4090 if (q == ldata + term->cols)
374330e2 4091 q--;
32874aea 4092 if (p.x >= q - ldata)
887035a5 4093 p.x = (dir == -1 ? q - ldata : term->cols - 1);
374330e2 4094 }
4095 break;
4096 case SM_WORD:
4097 /*
4098 * In this mode, the units are maximal runs of characters
4099 * whose `wordness' has the same value.
4100 */
3c7366f8 4101 wvalue = wordtype(term, UCSGET(ldata, p.x));
374330e2 4102 if (dir == +1) {
50ab4088 4103 while (1) {
3c7366f8 4104 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
4105 term->cols-1 : term->cols);
4106 if (p.x < maxcols-1) {
4107 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
50ab4088 4108 p.x++;
4109 else
4110 break;
4111 } else {
887035a5 4112 if (ldata[term->cols] & LATTR_WRAPPED) {
50ab4088 4113 unsigned long *ldata2;
4114 ldata2 = lineptr(p.y+1);
3c7366f8 4115 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
50ab4088 4116 p.x = 0;
4117 p.y++;
4118 ldata = ldata2;
4119 } else
4120 break;
4121 } else
4122 break;
4123 }
4124 }
374330e2 4125 } else {
50ab4088 4126 while (1) {
4127 if (p.x > 0) {
3c7366f8 4128 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
50ab4088 4129 p.x--;
4130 else
4131 break;
4132 } else {
4133 unsigned long *ldata2;
3c7366f8 4134 int maxcols;
50ab4088 4135 if (p.y <= topy)
4136 break;
4137 ldata2 = lineptr(p.y-1);
3c7366f8 4138 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
4139 term->cols-1 : term->cols);
4140 if (ldata2[term->cols] & LATTR_WRAPPED) {
4141 if (wordtype(term, UCSGET(ldata2, maxcols-1))
4142 == wvalue) {
4143 p.x = maxcols-1;
4144 p.y--;
4145 ldata = ldata2;
4146 } else
4147 break;
50ab4088 4148 } else
4149 break;
4150 }
4151 }
374330e2 4152 }
4153 break;
4154 case SM_LINE:
4155 /*
4156 * In this mode, every line is a unit.
4157 */
887035a5 4158 p.x = (dir == -1 ? 0 : term->cols - 1);
374330e2 4159 break;
4160 }
4161 return p;
4162}
4163
887035a5 4164static void sel_spread(Terminal *term)
32874aea 4165{
887035a5 4166 if (term->seltype == LEXICOGRAPHIC) {
4167 term->selstart = sel_spread_half(term, term->selstart, -1);
4168 decpos(term->selend);
4169 term->selend = sel_spread_half(term, term->selend, +1);
4170 incpos(term->selend);
6908fed7 4171 }
374330e2 4172}
4173
887035a5 4174void term_do_paste(Terminal *term)
568dd02f 4175{
4176 wchar_t *data;
4177 int len;
4178
a8327734 4179 get_clip(term->frontend, &data, &len);
2cb50250 4180 if (data && len > 0) {
568dd02f 4181 wchar_t *p, *q;
4182
887035a5 4183 term_seen_key_event(term); /* pasted data counts */
2cb50250 4184
887035a5 4185 if (term->paste_buffer)
4186 sfree(term->paste_buffer);
4187 term->paste_pos = term->paste_hold = term->paste_len = 0;
3d88e64d 4188 term->paste_buffer = snewn(len, wchar_t);
568dd02f 4189
4190 p = q = data;
4191 while (p < data + len) {
4192 while (p < data + len &&
4193 !(p <= data + len - sel_nl_sz &&
4194 !memcmp(p, sel_nl, sizeof(sel_nl))))
4195 p++;
4196
4197 {
4198 int i;
4199 for (i = 0; i < p - q; i++) {
887035a5 4200 term->paste_buffer[term->paste_len++] = q[i];
568dd02f 4201 }
4202 }
4203
4204 if (p <= data + len - sel_nl_sz &&
4205 !memcmp(p, sel_nl, sizeof(sel_nl))) {
92e23ad9 4206 term->paste_buffer[term->paste_len++] = '\015';
568dd02f 4207 p += sel_nl_sz;
4208 }
4209 q = p;
4210 }
4211
4212 /* Assume a small paste will be OK in one go. */
887035a5 4213 if (term->paste_len < 256) {
b9d7bcad 4214 if (term->ldisc)
4215 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
887035a5 4216 if (term->paste_buffer)
4217 sfree(term->paste_buffer);
4218 term->paste_buffer = 0;
4219 term->paste_pos = term->paste_hold = term->paste_len = 0;
568dd02f 4220 }
4221 }
a8327734 4222 get_clip(term->frontend, NULL, NULL);
568dd02f 4223}
4224
fc5b0934 4225void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4226 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
32874aea 4227{
4facdf84 4228 pos selpoint;
4229 unsigned long *ldata;
887035a5 4230 int raw_mouse = (term->xterm_mouse &&
64734920 4231 !term->cfg.no_mouse_rep &&
4232 !(term->cfg.mouse_override && shift));
6908fed7 4233 int default_seltype;
32874aea 4234
fff61f25 4235 if (y < 0) {
32874aea 4236 y = 0;
fff61f25 4237 if (a == MA_DRAG && !raw_mouse)
887035a5 4238 term_scroll(term, 0, -1);
fff61f25 4239 }
887035a5 4240 if (y >= term->rows) {
4241 y = term->rows - 1;
fff61f25 4242 if (a == MA_DRAG && !raw_mouse)
887035a5 4243 term_scroll(term, 0, +1);
fff61f25 4244 }
32874aea 4245 if (x < 0) {
4246 if (y > 0) {
887035a5 4247 x = term->cols - 1;
32874aea 4248 y--;
4249 } else
4250 x = 0;
094ed2a6 4251 }
887035a5 4252 if (x >= term->cols)
4253 x = term->cols - 1;
37508af4 4254
887035a5 4255 selpoint.y = y + term->disptop;
4facdf84 4256 selpoint.x = x;
4257 ldata = lineptr(selpoint.y);
887035a5 4258 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
4facdf84 4259 selpoint.x /= 2;
374330e2 4260
fff61f25 4261 if (raw_mouse) {
01c034ad 4262 int encstate = 0, r, c;
4263 char abuf[16];
01c034ad 4264
b9d7bcad 4265 if (term->ldisc) {
4266
fc5b0934 4267 switch (braw) {
b9d7bcad 4268 case MBT_LEFT:
4269 encstate = 0x20; /* left button down */
4270 break;
4271 case MBT_MIDDLE:
4272 encstate = 0x21;
4273 break;
4274 case MBT_RIGHT:
4275 encstate = 0x22;
4276 break;
4277 case MBT_WHEEL_UP:
4278 encstate = 0x60;
4279 break;
4280 case MBT_WHEEL_DOWN:
4281 encstate = 0x61;
4282 break;
4283 default: break; /* placate gcc warning about enum use */
4284 }
4285 switch (a) {
4286 case MA_DRAG:
4287 if (term->xterm_mouse == 1)
4288 return;
4289 encstate += 0x20;
4290 break;
4291 case MA_RELEASE:
4292 encstate = 0x23;
4293 term->mouse_is_down = 0;
4294 break;
4295 case MA_CLICK:
fc5b0934 4296 if (term->mouse_is_down == braw)
b9d7bcad 4297 return;
fc5b0934 4298 term->mouse_is_down = braw;
b9d7bcad 4299 break;
4300 default: break; /* placate gcc warning about enum use */
4301 }
4302 if (shift)
4303 encstate += 0x04;
4304 if (ctrl)
4305 encstate += 0x10;
4306 r = y + 33;
4307 c = x + 33;
4308
4309 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4310 ldisc_send(term->ldisc, abuf, 6, 0);
01c034ad 4311 }
01c034ad 4312 return;
4313 }
4314
6908fed7 4315 /*
4316 * Set the selection type (rectangular or normal) at the start
4317 * of a selection attempt, from the state of Alt.
4318 */
64734920 4319 if (!alt ^ !term->cfg.rect_select)
6908fed7 4320 default_seltype = RECTANGULAR;
4321 else
4322 default_seltype = LEXICOGRAPHIC;
4323
887035a5 4324 if (term->selstate == NO_SELECTION) {
4325 term->seltype = default_seltype;
6908fed7 4326 }
4327
fc5b0934 4328 if (bcooked == MBT_SELECT && a == MA_CLICK) {
887035a5 4329 deselect(term);
4330 term->selstate = ABOUT_TO;
4331 term->seltype = default_seltype;
4332 term->selanchor = selpoint;
4333 term->selmode = SM_CHAR;
fc5b0934 4334 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
887035a5 4335 deselect(term);
4336 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4337 term->selstate = DRAGGING;
4338 term->selstart = term->selanchor = selpoint;
4339 term->selend = term->selstart;
4340 incpos(term->selend);
4341 sel_spread(term);
fc5b0934 4342 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4343 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
887035a5 4344 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
374330e2 4345 return;
fc5b0934 4346 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4347 term->selstate == SELECTED) {
887035a5 4348 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 4349 /*
4350 * For normal selection, we extend by moving
4351 * whichever end of the current selection is closer
4352 * to the mouse.
4353 */
887035a5 4354 if (posdiff(selpoint, term->selstart) <
4355 posdiff(term->selend, term->selstart) / 2) {
4356 term->selanchor = term->selend;
4357 decpos(term->selanchor);
6908fed7 4358 } else {
887035a5 4359 term->selanchor = term->selstart;
6908fed7 4360 }
4facdf84 4361 } else {
6908fed7 4362 /*
4363 * For rectangular selection, we have a choice of
4364 * _four_ places to put selanchor and selpoint: the
4365 * four corners of the selection.
4366 */
887035a5 4367 if (2*selpoint.x < term->selstart.x + term->selend.x)
4368 term->selanchor.x = term->selend.x-1;
6908fed7 4369 else
887035a5 4370 term->selanchor.x = term->selstart.x;
6908fed7 4371
887035a5 4372 if (2*selpoint.y < term->selstart.y + term->selend.y)
4373 term->selanchor.y = term->selend.y;
6908fed7 4374 else
887035a5 4375 term->selanchor.y = term->selstart.y;
4facdf84 4376 }
887035a5 4377 term->selstate = DRAGGING;
374330e2 4378 }
887035a5 4379 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4380 term->selanchor = selpoint;
4381 term->selstate = DRAGGING;
4382 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 4383 /*
4384 * For normal selection, we set (selstart,selend) to
4385 * (selpoint,selanchor) in some order.
4386 */
887035a5 4387 if (poslt(selpoint, term->selanchor)) {
4388 term->selstart = selpoint;
4389 term->selend = term->selanchor;
4390 incpos(term->selend);
6908fed7 4391 } else {
887035a5 4392 term->selstart = term->selanchor;
4393 term->selend = selpoint;
4394 incpos(term->selend);
6908fed7 4395 }
374330e2 4396 } else {
6908fed7 4397 /*
4398 * For rectangular selection, we may need to
4399 * interchange x and y coordinates (if the user has
4400 * dragged in the -x and +y directions, or vice versa).
4401 */
887035a5 4402 term->selstart.x = min(term->selanchor.x, selpoint.x);
4403 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4404 term->selstart.y = min(term->selanchor.y, selpoint.y);
4405 term->selend.y = max(term->selanchor.y, selpoint.y);
374330e2 4406 }
887035a5 4407 sel_spread(term);
fc5b0934 4408 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4409 a == MA_RELEASE) {
887035a5 4410 if (term->selstate == DRAGGING) {
374330e2 4411 /*
4412 * We've completed a selection. We now transfer the
4413 * data to the clipboard.
4414 */
887035a5 4415 clipme(term, term->selstart, term->selend,
c076966d 4416 (term->seltype == RECTANGULAR), FALSE);
887035a5 4417 term->selstate = SELECTED;
374330e2 4418 } else
887035a5 4419 term->selstate = NO_SELECTION;
fc5b0934 4420 } else if (bcooked == MBT_PASTE
e6346999 4421 && (a == MA_CLICK
4422#if MULTICLICK_ONLY_EVENT
4423 || a == MA_2CLK || a == MA_3CLK
4424#endif
4425 )) {
a8327734 4426 request_paste(term->frontend);
374330e2 4427 }
4428
887035a5 4429 term_update(term);
374330e2 4430}
4431
6c50d421 4432void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4433 unsigned int modifiers, unsigned int flags)
4434{
4435 char output[10];
4436 char *p = output;
4437 int prependesc = FALSE;
84b8d085 4438#if 0
6c50d421 4439 int i;
4440
4441 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4442 for (i = 0; i < tlen; i++)
bf02e82e 4443 fprintf(stderr, " %04x", (unsigned)text[i]);
6c50d421 4444 fprintf(stderr, "\n");
84b8d085 4445#endif
6c50d421 4446
4447 /* XXX Num Lock */
4448 if ((flags & PKF_REPEAT) && term->repeat_off)
4449 return;
4450
4451 /* Currently, Meta always just prefixes everything with ESC. */
4452 if (modifiers & PKM_META)
4453 prependesc = TRUE;
4454 modifiers &= ~PKM_META;
4455
4456 /*
4457 * Alt is only used for Alt+keypad, which isn't supported yet, so
4458 * ignore it.
4459 */
4460 modifiers &= ~PKM_ALT;
4461
4462 /* Standard local function keys */
4463 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4464 case PKM_SHIFT:
4465 if (keysym == PK_PAGEUP)
4466 /* scroll up one page */;
4467 if (keysym == PK_PAGEDOWN)
4468 /* scroll down on page */;
4469 if (keysym == PK_INSERT)
4470 term_do_paste(term);
4471 break;
4472 case PKM_CONTROL:
4473 if (keysym == PK_PAGEUP)
4474 /* scroll up one line */;
4475 if (keysym == PK_PAGEDOWN)
4476 /* scroll down one line */;
4477 /* Control-Numlock for app-keypad mode switch */
4478 if (keysym == PK_PF1)
4479 term->app_keypad_keys ^= 1;
4480 break;
4481 }
4482
4483 if (modifiers & PKM_ALT) {
4484 /* Alt+F4 (close) */
4485 /* Alt+Return (full screen) */
4486 /* Alt+Space (system menu) */
4487 }
4488
4489 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4490 text[0] >= 0x20 && text[0] <= 0x7e) {
4491 /* ASCII chars + Control */
bf02e82e 4492 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4493 (text[0] >= 0x61 && text[0] <= 0x7a))
6c50d421 4494 text[0] &= 0x1f;
4495 else {
4496 /*
4497 * Control-2 should return ^@ (0x00), Control-6 should return
4498 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4499 * the DOS keyboard handling did it, and we have nothing better
4500 * to do with the key combo in question, we'll also map
4501 * Control-Backquote to ^\ (0x1C).
4502 */
4503 switch (text[0]) {
4504 case ' ': text[0] = 0x00; break;
4505 case '-': text[0] = 0x1f; break;
4506 case '/': text[0] = 0x1f; break;
4507 case '2': text[0] = 0x00; break;
4508 case '3': text[0] = 0x1b; break;
4509 case '4': text[0] = 0x1c; break;
4510 case '5': text[0] = 0x1d; break;
4511 case '6': text[0] = 0x1e; break;
4512 case '7': text[0] = 0x1f; break;
4513 case '8': text[0] = 0x7f; break;
4514 case '`': text[0] = 0x1c; break;
4515 }
4516 }
4517 }
4518
4519 /* Nethack keypad */
4520 if (term->cfg.nethack_keypad) {
4521 char c = 0;
4522 switch (keysym) {
4523 case PK_KP1: c = 'b'; break;
4524 case PK_KP2: c = 'j'; break;
4525 case PK_KP3: c = 'n'; break;
4526 case PK_KP4: c = 'h'; break;
4527 case PK_KP5: c = '.'; break;
4528 case PK_KP6: c = 'l'; break;
4529 case PK_KP7: c = 'y'; break;
4530 case PK_KP8: c = 'k'; break;
4531 case PK_KP9: c = 'u'; break;
bf02e82e 4532 default: break; /* else gcc warns `enum value not used' */
6c50d421 4533 }
4534 if (c != 0) {
4535 if (c != '.') {
4536 if (modifiers & PKM_CONTROL)
4537 c &= 0x1f;
4538 else if (modifiers & PKM_SHIFT)
4539 c = toupper(c);
4540 }
4541 *p++ = c;
4542 goto done;
4543 }
4544 }
4545
4546 /* Numeric Keypad */
4547 if (PK_ISKEYPAD(keysym)) {
4548 int xkey = 0;
4549
4550 /*
4551 * In VT400 mode, PFn always emits an escape sequence. In
4552 * Linux and tilde modes, this only happens in app keypad mode.
4553 */
4554 if (term->cfg.funky_type == FUNKY_VT400 ||
4555 ((term->cfg.funky_type == FUNKY_LINUX ||
4556 term->cfg.funky_type == FUNKY_TILDE) &&
4557 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4558 switch (keysym) {
4559 case PK_PF1: xkey = 'P'; break;
4560 case PK_PF2: xkey = 'Q'; break;
4561 case PK_PF3: xkey = 'R'; break;
4562 case PK_PF4: xkey = 'S'; break;
bf02e82e 4563 default: break; /* else gcc warns `enum value not used' */
6c50d421 4564 }
4565 }
4566 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4567 switch (keysym) {
4568 case PK_KP0: xkey = 'p'; break;
4569 case PK_KP1: xkey = 'q'; break;
4570 case PK_KP2: xkey = 'r'; break;
4571 case PK_KP3: xkey = 's'; break;
4572 case PK_KP4: xkey = 't'; break;
4573 case PK_KP5: xkey = 'u'; break;
4574 case PK_KP6: xkey = 'v'; break;
4575 case PK_KP7: xkey = 'w'; break;
4576 case PK_KP8: xkey = 'x'; break;
4577 case PK_KP9: xkey = 'y'; break;
4578 case PK_KPDECIMAL: xkey = 'n'; break;
4579 case PK_KPENTER: xkey = 'M'; break;
bf02e82e 4580 default: break; /* else gcc warns `enum value not used' */
6c50d421 4581 }
4582 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4583 /*
4584 * xterm can't see the layout of the keypad, so it has
4585 * to rely on the X keysyms returned by the keys.
4586 * Hence, we look at the strings here, not the PuTTY
4587 * keysyms (which describe the layout).
4588 */
4589 switch (text[0]) {
4590 case '+':
4591 if (modifiers & PKM_SHIFT)
4592 xkey = 'l';
4593 else
4594 xkey = 'k';
4595 break;
4596 case '/': xkey = 'o'; break;
4597 case '*': xkey = 'j'; break;
4598 case '-': xkey = 'm'; break;
4599 }
4600 } else {
4601 /*
4602 * In all other modes, we try to retain the layout of
4603 * the DEC keypad in application mode.
4604 */
4605 switch (keysym) {
4606 case PK_KPBIGPLUS:
4607 /* This key covers the '-' and ',' keys on a VT220 */
4608 if (modifiers & PKM_SHIFT)
4609 xkey = 'm'; /* VT220 '-' */
4610 else
4611 xkey = 'l'; /* VT220 ',' */
4612 break;
4613 case PK_KPMINUS: xkey = 'm'; break;
4614 case PK_KPCOMMA: xkey = 'l'; break;
bf02e82e 4615 default: break; /* else gcc warns `enum value not used' */
6c50d421 4616 }
4617 }
4618 }
4619 if (xkey) {
4620 if (term->vt52_mode) {
4621 if (xkey >= 'P' && xkey <= 'S')
4622 p += sprintf((char *) p, "\x1B%c", xkey);
4623 else
4624 p += sprintf((char *) p, "\x1B?%c", xkey);
4625 } else
4626 p += sprintf((char *) p, "\x1BO%c", xkey);
4627 goto done;
4628 }
4629 /* Not in application mode -- treat the number pad as arrow keys? */
4630 if ((flags & PKF_NUMLOCK) == 0) {
4631 switch (keysym) {
4632 case PK_KP0: keysym = PK_INSERT; break;
4633 case PK_KP1: keysym = PK_END; break;
4634 case PK_KP2: keysym = PK_DOWN; break;
4635 case PK_KP3: keysym = PK_PAGEDOWN; break;
4636 case PK_KP4: keysym = PK_LEFT; break;
4637 case PK_KP5: keysym = PK_REST; break;
4638 case PK_KP6: keysym = PK_RIGHT; break;
4639 case PK_KP7: keysym = PK_HOME; break;
4640 case PK_KP8: keysym = PK_UP; break;
4641 case PK_KP9: keysym = PK_PAGEUP; break;
bf02e82e 4642 default: break; /* else gcc warns `enum value not used' */
6c50d421 4643 }
4644 }
4645 }
4646
4647 /* Miscellaneous keys */
4648 switch (keysym) {
4649 case PK_ESCAPE:
4650 *p++ = 0x1b;
4651 goto done;
4652 case PK_BACKSPACE:
4653 if (modifiers == 0)
4654 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4655 else if (modifiers == PKM_SHIFT)
4656 /* We do the opposite of what is configured */
4657 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4658 else break;
4659 goto done;
4660 case PK_TAB:
4661 if (modifiers == 0)
4662 *p++ = 0x09;
4663 else if (modifiers == PKM_SHIFT)
4664 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4665 else break;
4666 goto done;
4667 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4668 case PK_PAUSE:
4669 if (modifiers == PKM_CONTROL)
4670 *p++ = 26;
4671 else break;
4672 goto done;
4673 case PK_RETURN:
4674 case PK_KPENTER: /* Odd keypad modes handled above */
4675 if (modifiers == 0) {
4676 *p++ = 0x0d;
4677 if (term->cr_lf_return)
4678 *p++ = 0x0a;
4679 goto done;
4680 }
bf02e82e 4681 default: break; /* else gcc warns `enum value not used' */
6c50d421 4682 }
4683
4684 /* SCO function keys and editing keys */
4685 if (term->cfg.funky_type == FUNKY_SCO) {
4686 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4687 static char const codes[] =
4688 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4689 int index = keysym - PK_F1;
4690
4691 if (modifiers & PKM_SHIFT) index += 12;
4692 if (modifiers & PKM_CONTROL) index += 24;
4693 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4694 goto done;
4695 }
4696 if (PK_ISEDITING(keysym)) {
4697 int xkey = 0;
4698
4699 switch (keysym) {
4700 case PK_DELETE: *p++ = 0x7f; goto done;
4701 case PK_HOME: xkey = 'H'; break;
4702 case PK_INSERT: xkey = 'L'; break;
4703 case PK_END: xkey = 'F'; break;
4704 case PK_PAGEUP: xkey = 'I'; break;
4705 case PK_PAGEDOWN: xkey = 'G'; break;
bf02e82e 4706 default: break; /* else gcc warns `enum value not used' */
6c50d421 4707 }
4708 p += sprintf((char *) p, "\x1B[%c", xkey);
4709 }
4710 }
4711
4712 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4713 int code;
4714
4715 if (term->cfg.funky_type == FUNKY_XTERM) {
4716 /* Xterm shuffles these keys, apparently. */
4717 switch (keysym) {
4718 case PK_HOME: keysym = PK_INSERT; break;
4719 case PK_INSERT: keysym = PK_HOME; break;
4720 case PK_DELETE: keysym = PK_END; break;
4721 case PK_END: keysym = PK_PAGEUP; break;
4722 case PK_PAGEUP: keysym = PK_DELETE; break;
4723 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
bf02e82e 4724 default: break; /* else gcc warns `enum value not used' */
6c50d421 4725 }
4726 }
4727
4728 /* RXVT Home/End */
4729 if (term->cfg.rxvt_homeend &&
4730 (keysym == PK_HOME || keysym == PK_END)) {
4731 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4732 goto done;
4733 }
4734
4735 if (term->vt52_mode) {
4736 int xkey;
4737
4738 /*
4739 * A real VT52 doesn't have these, and a VT220 doesn't
4740 * send anything for them in VT52 mode.
4741 */
4742 switch (keysym) {
4743 case PK_HOME: xkey = 'H'; break;
4744 case PK_INSERT: xkey = 'L'; break;
4745 case PK_DELETE: xkey = 'M'; break;
4746 case PK_END: xkey = 'E'; break;
4747 case PK_PAGEUP: xkey = 'I'; break;
4748 case PK_PAGEDOWN: xkey = 'G'; break;
7ffdbc1a 4749 default: xkey=0; break; /* else gcc warns `enum value not used'*/
6c50d421 4750 }
4751 p += sprintf((char *) p, "\x1B%c", xkey);
4752 goto done;
4753 }
4754
4755 switch (keysym) {
4756 case PK_HOME: code = 1; break;
4757 case PK_INSERT: code = 2; break;
4758 case PK_DELETE: code = 3; break;
4759 case PK_END: code = 4; break;
4760 case PK_PAGEUP: code = 5; break;
4761 case PK_PAGEDOWN: code = 6; break;
7ffdbc1a 4762 default: code = 0; break; /* else gcc warns `enum value not used' */
6c50d421 4763 }
4764 p += sprintf((char *) p, "\x1B[%d~", code);
4765 goto done;
4766 }
4767
4768 if (PK_ISFKEY(keysym)) {
4769 /* Map Shift+F1-F10 to F11-F20 */
4770 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4771 keysym += 10;
4772 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4773 keysym <= PK_F14) {
4774 /* XXX This overrides the XTERM/VT52 mode below */
4775 int offt = 0;
4776 if (keysym >= PK_F6) offt++;
4777 if (keysym >= PK_F12) offt++;
4778 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4779 'P' + keysym - PK_F1 - offt);
4780 goto done;
4781 }
4782 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4783 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4784 goto done;
4785 }
4786 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4787 if (term->vt52_mode)
4788 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4789 else
4790 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4791 goto done;
4792 }
4793 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4794 goto done;
4795 }
4796
4797 if (PK_ISCURSOR(keysym)) {
4798 int xkey;
4799
4800 switch (keysym) {
4801 case PK_UP: xkey = 'A'; break;
4802 case PK_DOWN: xkey = 'B'; break;
4803 case PK_RIGHT: xkey = 'C'; break;
4804 case PK_LEFT: xkey = 'D'; break;
4805 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5342c096 4806 default: xkey = 0; break; /* else gcc warns `enum value not used' */
6c50d421 4807 }
4808 if (term->vt52_mode)
4809 p += sprintf((char *) p, "\x1B%c", xkey);
4810 else {
4811 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4812
4813 /* Useful mapping of Ctrl-arrows */
4814 if (modifiers == PKM_CONTROL)
4815 app_flg = !app_flg;
4816
4817 if (app_flg)
4818 p += sprintf((char *) p, "\x1BO%c", xkey);
4819 else
4820 p += sprintf((char *) p, "\x1B[%c", xkey);
4821 }
4822 goto done;
4823 }
4824
4825 done:
4826 if (p > output || tlen > 0) {
4827 /*
4828 * Interrupt an ongoing paste. I'm not sure
4829 * this is sensible, but for the moment it's
4830 * preferable to having to faff about buffering
4831 * things.
4832 */
4833 term_nopaste(term);
4834
4835 /*
4836 * We need not bother about stdin backlogs
4837 * here, because in GUI PuTTY we can't do
4838 * anything about it anyway; there's no means
4839 * of asking Windows to hold off on KEYDOWN
4840 * messages. We _have_ to buffer everything
4841 * we're sent.
4842 */
4843 term_seen_key_event(term);
4844
4845 if (prependesc) {
84b8d085 4846#if 0
6c50d421 4847 fprintf(stderr, "sending ESC\n");
84b8d085 4848#endif
6c50d421 4849 ldisc_send(term->ldisc, "\x1b", 1, 1);
4850 }
4851
4852 if (p > output) {
84b8d085 4853#if 0
6c50d421 4854 fprintf(stderr, "sending %d bytes:", p - output);
4855 for (i = 0; i < p - output; i++)
4856 fprintf(stderr, " %02x", output[i]);
4857 fprintf(stderr, "\n");
84b8d085 4858#endif
6c50d421 4859 ldisc_send(term->ldisc, output, p - output, 1);
4860 } else if (tlen > 0) {
84b8d085 4861#if 0
6c50d421 4862 fprintf(stderr, "sending %d unichars:", tlen);
4863 for (i = 0; i < tlen; i++)
bf02e82e 4864 fprintf(stderr, " %04x", (unsigned) text[i]);
6c50d421 4865 fprintf(stderr, "\n");
84b8d085 4866#endif
6c50d421 4867 luni_send(term->ldisc, text, tlen, 1);
4868 }
4869 }
4870}
4871
887035a5 4872void term_nopaste(Terminal *term)
32874aea 4873{
887035a5 4874 if (term->paste_len == 0)
32874aea 4875 return;
887035a5 4876 sfree(term->paste_buffer);
f278d6f8 4877 term->paste_buffer = NULL;
887035a5 4878 term->paste_len = 0;
c9def1b8 4879}
4880
887035a5 4881int term_paste_pending(Terminal *term)
0f660c8f 4882{
887035a5 4883 return term->paste_len != 0;
0f660c8f 4884}
4885
887035a5 4886void term_paste(Terminal *term)
32874aea 4887{
c9def1b8 4888 long now, paste_diff;
4889
887035a5 4890 if (term->paste_len == 0)
32874aea 4891 return;
c9def1b8 4892
4893 /* Don't wait forever to paste */
887035a5 4894 if (term->paste_hold) {
f7f27309 4895 now = GETTICKCOUNT();
887035a5 4896 paste_diff = now - term->last_paste;
32874aea 4897 if (paste_diff >= 0 && paste_diff < 450)
c9def1b8 4898 return;
4899 }
887035a5 4900 term->paste_hold = 0;
c9def1b8 4901
887035a5 4902 while (term->paste_pos < term->paste_len) {
8df7a775 4903 int n = 0;
887035a5 4904 while (n + term->paste_pos < term->paste_len) {
92e23ad9 4905 if (term->paste_buffer[term->paste_pos + n++] == '\015')
8df7a775 4906 break;
4907 }
b9d7bcad 4908 if (term->ldisc)
4909 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
887035a5 4910 term->paste_pos += n;
c9def1b8 4911
887035a5 4912 if (term->paste_pos < term->paste_len) {
4913 term->paste_hold = 1;
c9def1b8 4914 return;
4915 }
4916 }
887035a5 4917 sfree(term->paste_buffer);
4918 term->paste_buffer = NULL;
4919 term->paste_len = 0;
c9def1b8 4920}
4921
887035a5 4922static void deselect(Terminal *term)
32874aea 4923{
887035a5 4924 term->selstate = NO_SELECTION;
4925 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
374330e2 4926}
4927
887035a5 4928void term_deselect(Terminal *term)
32874aea 4929{
887035a5 4930 deselect(term);
4931 term_update(term);
374330e2 4932}
fe50e814 4933
887035a5 4934int term_ldisc(Terminal *term, int option)
32874aea 4935{
4936 if (option == LD_ECHO)
887035a5 4937 return term->term_echoing;
32874aea 4938 if (option == LD_EDIT)
887035a5 4939 return term->term_editing;
0965bee0 4940 return FALSE;
4941}
4942
fbf6cb3b 4943int term_data(Terminal *term, int is_stderr, const char *data, int len)
32874aea 4944{
887035a5 4945 bufchain_add(&term->inbuf, data, len);
5471d09a 4946
74aca06d 4947 if (!term->in_term_out) {
4948 term->in_term_out = TRUE;
4949 term_blink(term, 1);
4950 term_out(term);
4951 term->in_term_out = FALSE;
4952 }
4953
5471d09a 4954 /*
a748a096 4955 * term_out() always completely empties inbuf. Therefore,
4956 * there's no reason at all to return anything other than zero
4957 * from this function, because there _can't_ be a question of
4958 * the remote side needing to wait until term_out() has cleared
4959 * a backlog.
4960 *
5471d09a 4961 * This is a slightly suboptimal way to deal with SSH2 - in
4962 * principle, the window mechanism would allow us to continue
4963 * to accept data on forwarded ports and X connections even
4964 * while the terminal processing was going slowly - but we
4965 * can't do the 100% right thing without moving the terminal
4966 * processing into a separate thread, and that might hurt
4967 * portability. So we manage stdout buffering the old SSH1 way:
4968 * if the terminal processing goes slowly, the whole SSH
4969 * connection stops accepting data until it's ready.
a748a096 4970 *
5471d09a 4971 * In practice, I can't imagine this causing serious trouble.
4972 */
4973 return 0;
fe50e814 4974}
a8327734 4975
4976void term_provide_logctx(Terminal *term, void *logctx)
4977{
4978 term->logctx = logctx;
4979}