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