Add the ability to close sessions. This adds *_free() functions to most
[u/mdw/putty] / terminal.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4
5 #include <time.h>
6 #include <assert.h>
7 #include "putty.h"
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) )
21
22 #define VT52_PLUS
23
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 */
36
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)
45
46 #define compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
49 break; \
50 }
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
54 break; \
55 }
56
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
58
59 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
60 const wchar_t sel_nl[] = SEL_NL;
61
62 /*
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 /*
73 * Internal prototypes.
74 */
75 static unsigned long *resizeline(unsigned long *, int);
76 static unsigned long *lineptr(Terminal *, int, int);
77 static void do_paint(Terminal *, Context, int);
78 static void erase_lots(Terminal *, int, int, int);
79 static void swap_screen(Terminal *, int, int, int);
80 static void update_sbar(Terminal *);
81 static void deselect(Terminal *);
82 static void term_print_finish(Terminal *);
83 #ifdef OPTIMISE_SCROLL
84 static void scroll_display(Terminal *, int, int, int);
85 #endif /* OPTIMISE_SCROLL */
86
87 /*
88 * Resize a line to make it `cols' columns wide.
89 */
90 static unsigned long *resizeline(unsigned long *line, int cols)
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 /*
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 */
117 static unsigned long *lineptr(Terminal *term, int y, int lineno)
118 {
119 unsigned long *line, *newline;
120 tree234 *whichtree;
121 int treeindex;
122
123 if (y >= 0) {
124 whichtree = term->screen;
125 treeindex = y;
126 } else {
127 whichtree = term->scrollback;
128 treeindex = y + count234(term->scrollback);
129 }
130 line = index234(whichtree, treeindex);
131
132 /* We assume that we don't screw up and retrieve something out of range. */
133 assert(line != NULL);
134
135 newline = resizeline(line, term->cols);
136 if (newline != line) {
137 delpos234(whichtree, treeindex);
138 addpos234(whichtree, newline, treeindex);
139 line = newline;
140 }
141
142 return line + 1;
143 }
144
145 #define lineptr(x) lineptr(term,x,__LINE__)
146
147 /*
148 * Set up power-on settings for the terminal.
149 */
150 static void power_on(Terminal *term)
151 {
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;
158 else
159 term->alt_b = term->marg_b = 0;
160 if (term->cols != -1) {
161 int i;
162 for (i = 0; i < term->cols; i++)
163 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
164 }
165 term->alt_om = term->dec_om = term->cfg.dec_om;
166 term->alt_ins = term->insert = FALSE;
167 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
168 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
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;
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;
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;
184 term->erase_char = ERASE_CHAR;
185 term->alt_which = 0;
186 term_print_finish(term);
187 {
188 int i;
189 for (i = 0; i < 256; i++)
190 term->wordness[i] = term->cfg.wordness[i];
191 }
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);
197 }
198 }
199
200 /*
201 * Force a screen update.
202 */
203 void term_update(Terminal *term)
204 {
205 Context ctx;
206 ctx = get_ctx(term->frontend);
207 if (ctx) {
208 int need_sbar_update = term->seen_disp_event;
209 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
210 term->disptop = 0; /* return to main screen */
211 term->seen_disp_event = 0;
212 need_sbar_update = TRUE;
213 }
214 if (need_sbar_update)
215 update_sbar(term);
216 do_paint(term, ctx, TRUE);
217 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
218 free_ctx(ctx);
219 }
220 }
221
222 /*
223 * Called from front end when a keypress occurs, to trigger
224 * anything magical that needs to happen in that situation.
225 */
226 void term_seen_key_event(Terminal *term)
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 */
235 term->beep_overloaded = FALSE;
236 while (term->beephead) {
237 struct beeptime *tmp = term->beephead;
238 term->beephead = tmp->next;
239 sfree(tmp);
240 }
241 term->beeptail = NULL;
242 term->nbeeps = 0;
243
244 /*
245 * Reset the scrollback on keypress, if we're doing that.
246 */
247 if (term->cfg.scroll_on_key) {
248 term->disptop = 0; /* return to main screen */
249 term->seen_disp_event = 1;
250 }
251 }
252
253 /*
254 * Same as power_on(), but an external function.
255 */
256 void term_pwron(Terminal *term)
257 {
258 power_on(term);
259 if (term->ldisc) /* cause ldisc to notice changes */
260 ldisc_send(term->ldisc, NULL, 0, 0);
261 fix_cpos;
262 term->disptop = 0;
263 deselect(term);
264 term_update(term);
265 }
266
267 /*
268 * When the user reconfigures us, we need to check the forbidden-
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.
272 */
273 void term_reconfig(Terminal *term, Config *cfg)
274 {
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)
309 swap_screen(term, 0, FALSE, FALSE);
310 if (term->cfg.no_mouse_rep) {
311 term->xterm_mouse = 0;
312 set_raw_mouse_mode(term->frontend, 0);
313 }
314 if (term->cfg.no_remote_charset) {
315 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
316 term->sco_acs = term->alt_sco_acs = 0;
317 term->utf = 0;
318 }
319 if (!*term->cfg.printer) {
320 term_print_finish(term);
321 }
322 }
323
324 /*
325 * Clear the scrollback.
326 */
327 void term_clrsb(Terminal *term)
328 {
329 unsigned long *line;
330 term->disptop = 0;
331 while ((line = delpos234(term->scrollback, 0)) != NULL) {
332 sfree(line);
333 }
334 update_sbar(term);
335 }
336
337 /*
338 * Initialise the terminal.
339 */
340 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
341 void *frontend)
342 {
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));
350 term->frontend = frontend;
351 term->ucsdata = ucsdata;
352 term->cfg = *mycfg; /* STRUCTURE COPY */
353 term->logctx = NULL;
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;
358 term->paste_len = 0;
359 term->last_paste = 0;
360 bufchain_init(&term->inbuf);
361 bufchain_init(&term->printer_buf);
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;
367 term->xterm_mouse = term->mouse_is_down = FALSE;
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;
374 term->curstype = 0;
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;
387 term->attr_mask = 0xffffffff;
388 term->resize_fn = NULL;
389 term->resize_ctx = NULL;
390
391 return term;
392 }
393
394 void 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
422 /*
423 * Set up the terminal for a given size.
424 */
425 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
426 {
427 tree234 *newalt;
428 unsigned long *newdisp, *line;
429 int i, j;
430 int sblen;
431 int save_alt_which = term->alt_which;
432
433 if (newrows == term->rows && newcols == term->cols &&
434 newsavelines == term->savelines)
435 return; /* nothing to do */
436
437 deselect(term);
438 swap_screen(term, 0, FALSE, FALSE);
439
440 term->alt_t = term->marg_t = 0;
441 term->alt_b = term->marg_b = newrows - 1;
442
443 if (term->rows == -1) {
444 term->scrollback = newtree234(NULL);
445 term->screen = newtree234(NULL);
446 term->rows = 0;
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 */
469 sblen = count234(term->scrollback);
470 /* Do this loop to expand the screen if newrows > rows */
471 for (i = term->rows; i < newrows; i++) {
472 if (sblen > 0) {
473 line = delpos234(term->scrollback, --sblen);
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 }
480 addpos234(term->screen, line, 0);
481 }
482 /* Do this loop to shrink the screen if newrows < rows */
483 for (i = newrows; i < term->rows; i++) {
484 line = delpos234(term->screen, 0);
485 addpos234(term->scrollback, line, sblen++);
486 }
487 assert(count234(term->screen) == newrows);
488 while (sblen > newsavelines) {
489 line = delpos234(term->scrollback, 0);
490 sfree(line);
491 sblen--;
492 }
493 assert(count234(term->scrollback) <= newsavelines);
494 term->disptop = 0;
495
496 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
497 for (i = 0; i < newrows * (newcols + 1); i++)
498 newdisp[i] = ATTR_INVALID;
499 sfree(term->disptext);
500 term->disptext = newdisp;
501 term->dispcurs = NULL;
502
503 newalt = newtree234(NULL);
504 for (i = 0; i < newrows; i++) {
505 line = smalloc(TSIZE * (newcols + 2));
506 line[0] = newcols;
507 for (j = 0; j <= newcols; j++)
508 line[j + 1] = term->erase_char;
509 addpos234(newalt, line, i);
510 }
511 if (term->alt_screen) {
512 while (NULL != (line = delpos234(term->alt_screen, 0)))
513 sfree(line);
514 freetree234(term->alt_screen);
515 }
516 term->alt_screen = newalt;
517
518 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
519 {
520 int i;
521 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
522 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
523 }
524
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;
539 fix_cpos;
540
541 swap_screen(term, save_alt_which, FALSE, FALSE);
542
543 update_sbar(term);
544 term_update(term);
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 */
553 void 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);
561 }
562
563 /*
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.)
569 */
570 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
571 {
572 int t;
573 tree234 *ttr;
574
575 if (!which)
576 reset = FALSE; /* do no weird resetting if which==0 */
577
578 if (which != term->alt_which) {
579 term->alt_which = which;
580
581 ttr = term->alt_screen;
582 term->alt_screen = term->screen;
583 term->screen = ttr;
584 t = term->curs.x;
585 if (!reset && !keep_cur_pos)
586 term->curs.x = term->alt_x;
587 term->alt_x = t;
588 t = term->curs.y;
589 if (!reset && !keep_cur_pos)
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;
619 }
620
621 if (reset && term->screen) {
622 /*
623 * Yes, this _is_ supposed to honour background-colour-erase.
624 */
625 erase_lots(term, FALSE, TRUE, TRUE);
626 }
627
628 /*
629 * This might not be possible if we're called during
630 * initialisation.
631 */
632 if (term->screen)
633 fix_cpos;
634 }
635
636 /*
637 * Update the scroll bar.
638 */
639 static void update_sbar(Terminal *term)
640 {
641 int nscroll;
642
643 nscroll = count234(term->scrollback);
644
645 set_sbar(term->frontend, nscroll + term->rows,
646 nscroll + term->disptop, term->rows);
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 */
653 static void check_selection(Terminal *term, pos from, pos to)
654 {
655 if (poslt(from, term->selend) && poslt(term->selstart, to))
656 deselect(term);
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.
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.
668 */
669 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
670 {
671 unsigned long *line, *line2;
672 int i, seltop, olddisptop, shift;
673
674 if (topline != 0 || term->alt_which != 0)
675 sb = FALSE;
676
677 olddisptop = term->disptop;
678 shift = lines;
679 if (lines < 0) {
680 while (lines < 0) {
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;
693 }
694 }
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;
700 }
701 }
702
703 lines++;
704 }
705 } else {
706 while (lines > 0) {
707 line = delpos234(term->screen, topline);
708 if (sb && term->savelines > 0) {
709 int sblen = count234(term->scrollback);
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 */
716 if (sblen == term->savelines) {
717 sblen--, line2 = delpos234(term->scrollback, 0);
718 } else {
719 line2 = smalloc(TSIZE * (term->cols + 2));
720 line2[0] = term->cols;
721 }
722 addpos234(term->scrollback, line, sblen);
723 line = line2;
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 */
739 if (term->disptop > -term->savelines && term->disptop < 0)
740 term->disptop--;
741 }
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);
747
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.
754 *
755 * This applies to selstart and selend (for an existing
756 * selection), and also selanchor (for one being
757 * selected as we speak).
758 */
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;
767 }
768 }
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;
774 }
775 }
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;
781 }
782 }
783
784 lines--;
785 }
786 }
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 */
792 }
793
794 #ifdef OPTIMISE_SCROLL
795 /*
796 * Scroll the physical display, and our conception of it in disptext.
797 */
798 static 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);
809 if (term->dispcurs >= start + distance &&
810 term->dispcurs <= start + distance + size)
811 term->dispcurs -= distance;
812 for (i = 0; i < distance; i++)
813 (start + size)[i] |= ATTR_INVALID;
814 } else {
815 memmove(start + distance, start, size * TSIZE);
816 if (term->dispcurs >= start && term->dispcurs <= start + size)
817 term->dispcurs += distance;
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
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 */
831 static void move(Terminal *term, int x, int y, int marg_clip)
832 {
833 if (x < 0)
834 x = 0;
835 if (x >= term->cols)
836 x = term->cols - 1;
837 if (marg_clip) {
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;
844 }
845 if (y < 0)
846 y = 0;
847 if (y >= term->rows)
848 y = term->rows - 1;
849 term->curs.x = x;
850 term->curs.y = y;
851 fix_cpos;
852 term->wrapnext = FALSE;
853 }
854
855 /*
856 * Save or restore the cursor and SGR mode.
857 */
858 static void save_cursor(Terminal *term, int save)
859 {
860 if (save) {
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;
868 } else {
869 term->curs = term->savecurs;
870 /* Make sure the window hasn't shrunk since the save */
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;
880 /*
881 * wrapnext might reset to False if the x position is no
882 * longer at the rightmost edge.
883 */
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;
888 fix_cpos;
889 if (term->use_bce)
890 term->erase_char = (' ' | ATTR_ASCII |
891 (term->curr_attr &
892 (ATTR_FGMASK | ATTR_BGMASK)));
893 }
894 }
895
896 /*
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 */
915 static 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 /*
935 * Erase a large portion of the screen: the whole screen, or the
936 * whole line, or parts thereof.
937 */
938 static void erase_lots(Terminal *term,
939 int line_only, int from_begin, int to_end)
940 {
941 pos start, end;
942 int erase_lattr;
943 unsigned long *ldata;
944
945 if (line_only) {
946 start.y = term->curs.y;
947 start.x = 0;
948 end.y = term->curs.y + 1;
949 end.x = 0;
950 erase_lattr = FALSE;
951 } else {
952 start.y = 0;
953 start.x = 0;
954 end.y = term->rows;
955 end.x = 0;
956 erase_lattr = TRUE;
957 }
958 if (!from_begin) {
959 start = term->curs;
960 }
961 if (!to_end) {
962 end = term->curs;
963 incpos(end);
964 }
965 if (!from_begin || !to_end)
966 check_boundary(term, term->curs.x, term->curs.y);
967 check_selection(term, start, end);
968
969 /* Clear screen also forces a full window redraw, just in case. */
970 if (start.y == 0 && start.x == 0 && end.y == term->rows)
971 term_invalidate(term);
972
973 ldata = lineptr(start.y);
974 while (poslt(start, end)) {
975 if (start.x == term->cols && !erase_lattr)
976 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
977 else
978 ldata[start.x] = term->erase_char;
979 if (incpos(start) && start.y < term->rows)
980 ldata = lineptr(start.y);
981 }
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 */
988 static void insch(Terminal *term, int n)
989 {
990 int dir = (n < 0 ? -1 : +1);
991 int m;
992 pos cursplus;
993 unsigned long *ldata;
994
995 n = (n < 0 ? -n : n);
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);
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);
1005 ldata = lineptr(term->curs.y);
1006 if (dir < 0) {
1007 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1008 while (n--)
1009 ldata[term->curs.x + m++] = term->erase_char;
1010 } else {
1011 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1012 while (n--)
1013 ldata[term->curs.x + n] = term->erase_char;
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 */
1021 static void toggle_mode(Terminal *term, int mode, int query, int state)
1022 {
1023 unsigned long ticks;
1024
1025 if (query)
1026 switch (mode) {
1027 case 1: /* application cursor keys */
1028 term->app_cursor_keys = state;
1029 break;
1030 case 2: /* VT52 mode */
1031 term->vt52_mode = !state;
1032 if (term->vt52_mode) {
1033 term->blink_is_real = FALSE;
1034 term->vt52_bold = FALSE;
1035 } else {
1036 term->blink_is_real = term->cfg.blinktext;
1037 }
1038 break;
1039 case 3: /* 80/132 columns */
1040 deselect(term);
1041 if (!term->cfg.no_remote_resize)
1042 request_resize(term->frontend, state ? 132 : 80, term->rows);
1043 term->reset_132 = state;
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 */
1052 ticks = GETTICKCOUNT();
1053 /* turn off a previous vbell to avoid inconsistencies */
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*/
1058 /* If there's no vbell timeout already, or this one lasts
1059 * longer, replace vbell_timeout with ours. */
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) {
1066 /* This is an ON, so we notice the time and save it. */
1067 term->rvbell_startpoint = ticks;
1068 }
1069 term->rvideo = state;
1070 term->seen_disp_event = TRUE;
1071 if (state)
1072 term_update(term);
1073 break;
1074 case 6: /* DEC origin mode */
1075 term->dec_om = state;
1076 break;
1077 case 7: /* auto wrap */
1078 term->wrap = state;
1079 break;
1080 case 8: /* auto key repeat */
1081 term->repeat_off = !state;
1082 break;
1083 case 10: /* set local edit mode */
1084 term->term_editing = state;
1085 if (term->ldisc) /* cause ldisc to notice changes */
1086 ldisc_send(term->ldisc, NULL, 0, 0);
1087 break;
1088 case 25: /* enable/disable cursor */
1089 compatibility2(OTHER, VT220);
1090 term->cursor_on = state;
1091 term->seen_disp_event = TRUE;
1092 break;
1093 case 47: /* alternate screen */
1094 compatibility(OTHER);
1095 deselect(term);
1096 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1097 term->disptop = 0;
1098 break;
1099 case 1000: /* xterm mouse 1 */
1100 term->xterm_mouse = state ? 1 : 0;
1101 set_raw_mouse_mode(term->frontend, state);
1102 break;
1103 case 1002: /* xterm mouse 2 */
1104 term->xterm_mouse = state ? 2 : 0;
1105 set_raw_mouse_mode(term->frontend, state);
1106 break;
1107 case 1047: /* alternate screen */
1108 compatibility(OTHER);
1109 deselect(term);
1110 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1111 term->disptop = 0;
1112 break;
1113 case 1048: /* save/restore cursor */
1114 save_cursor(term, state);
1115 if (!state) term->seen_disp_event = TRUE;
1116 break;
1117 case 1049: /* cursor & alternate screen */
1118 if (state)
1119 save_cursor(term, state);
1120 if (!state) term->seen_disp_event = TRUE;
1121 compatibility(OTHER);
1122 deselect(term);
1123 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1124 if (!state)
1125 save_cursor(term, state);
1126 term->disptop = 0;
1127 break;
1128 } else
1129 switch (mode) {
1130 case 4: /* set insert mode */
1131 compatibility(VT102);
1132 term->insert = state;
1133 break;
1134 case 12: /* set echo mode */
1135 term->term_echoing = !state;
1136 if (term->ldisc) /* cause ldisc to notice changes */
1137 ldisc_send(term->ldisc, NULL, 0, 0);
1138 break;
1139 case 20: /* Return sends ... */
1140 term->cr_lf_return = state;
1141 break;
1142 case 34: /* Make cursor BIG */
1143 compatibility2(OTHER, VT220);
1144 term->big_cursor = !state;
1145 }
1146 }
1147
1148 /*
1149 * Process an OSC sequence: set window title or icon name.
1150 */
1151 static void do_osc(Terminal *term)
1152 {
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];
1157 } else {
1158 term->osc_string[term->osc_strlen] = '\0';
1159 switch (term->esc_args[0]) {
1160 case 0:
1161 case 1:
1162 if (!term->cfg.no_remote_wintitle)
1163 set_icon(term->frontend, term->osc_string);
1164 if (term->esc_args[0] == 1)
1165 break;
1166 /* fall through: parameter 0 means set both */
1167 case 2:
1168 case 21:
1169 if (!term->cfg.no_remote_wintitle)
1170 set_title(term->frontend, term->osc_string);
1171 break;
1172 }
1173 }
1174 }
1175
1176 /*
1177 * ANSI printing routines.
1178 */
1179 static void term_print_setup(Terminal *term)
1180 {
1181 bufchain_clear(&term->printer_buf);
1182 term->print_job = printer_start_job(term->cfg.printer);
1183 }
1184 static void term_print_flush(Terminal *term)
1185 {
1186 void *data;
1187 int len;
1188 int size;
1189 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1190 bufchain_prefix(&term->printer_buf, &data, &len);
1191 if (len > size-5)
1192 len = size-5;
1193 printer_job_data(term->print_job, data, len);
1194 bufchain_consume(&term->printer_buf, len);
1195 }
1196 }
1197 static void term_print_finish(Terminal *term)
1198 {
1199 void *data;
1200 int len, size;
1201 char c;
1202
1203 if (!term->printing && !term->only_printing)
1204 return; /* we need do nothing */
1205
1206 term_print_flush(term);
1207 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1208 bufchain_prefix(&term->printer_buf, &data, &len);
1209 c = *(char *)data;
1210 if (c == '\033' || c == '\233') {
1211 bufchain_consume(&term->printer_buf, size);
1212 break;
1213 } else {
1214 printer_job_data(term->print_job, &c, 1);
1215 bufchain_consume(&term->printer_buf, 1);
1216 }
1217 }
1218 printer_finish_job(term->print_job);
1219 term->print_job = NULL;
1220 term->printing = term->only_printing = FALSE;
1221 }
1222
1223 /*
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 */
1228 void term_out(Terminal *term)
1229 {
1230 int c, unget;
1231 unsigned char localbuf[256], *chars;
1232 int nchars = 0;
1233
1234 unget = -1;
1235
1236 chars = NULL; /* placate compiler warnings */
1237 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1238 if (unget == -1) {
1239 if (nchars == 0) {
1240 void *ret;
1241 bufchain_prefix(&term->inbuf, &ret, &nchars);
1242 if (nchars > sizeof(localbuf))
1243 nchars = sizeof(localbuf);
1244 memcpy(localbuf, ret, nchars);
1245 bufchain_consume(&term->inbuf, nchars);
1246 chars = localbuf;
1247 assert(chars != NULL);
1248 }
1249 c = *chars++;
1250 nchars--;
1251
1252 /*
1253 * Optionally log the session traffic to a file. Useful for
1254 * debugging and possibly also useful for actual logging.
1255 */
1256 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1257 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1258 } else {
1259 c = unget;
1260 unget = -1;
1261 }
1262
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 */
1267
1268 /*
1269 * If we're printing, add the character to the printer
1270 * buffer.
1271 */
1272 if (term->printing) {
1273 bufchain_add(&term->printer_buf, &c, 1);
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 */
1280 if (term->only_printing) {
1281 if (c == '\033')
1282 term->print_state = 1;
1283 else if (c == (unsigned char)'\233')
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;
1291 else
1292 term->print_state = 0;
1293 if (term->print_state == 4) {
1294 term_print_finish(term);
1295 }
1296 continue;
1297 }
1298 }
1299
1300 /* First see about all those translations. */
1301 if (term->termstate == TOPLEVEL) {
1302 if (in_utf(term))
1303 switch (term->utf_state) {
1304 case 0:
1305 if (c < 0x80) {
1306 /* UTF-8 must be stateless so we ignore iso2022. */
1307 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1308 c = term->ucsdata->unitab_ctrl[c];
1309 else c = ((unsigned char)c) | ATTR_ASCII;
1310 break;
1311 } else if ((c & 0xe0) == 0xc0) {
1312 term->utf_size = term->utf_state = 1;
1313 term->utf_char = (c & 0x1f);
1314 } else if ((c & 0xf0) == 0xe0) {
1315 term->utf_size = term->utf_state = 2;
1316 term->utf_char = (c & 0x0f);
1317 } else if ((c & 0xf8) == 0xf0) {
1318 term->utf_size = term->utf_state = 3;
1319 term->utf_char = (c & 0x07);
1320 } else if ((c & 0xfc) == 0xf8) {
1321 term->utf_size = term->utf_state = 4;
1322 term->utf_char = (c & 0x03);
1323 } else if ((c & 0xfe) == 0xfc) {
1324 term->utf_size = term->utf_state = 5;
1325 term->utf_char = (c & 0x01);
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) {
1337 unget = c;
1338 c = UCSERR;
1339 term->utf_state = 0;
1340 break;
1341 }
1342 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1343 if (--term->utf_state)
1344 continue;
1345
1346 c = term->utf_char;
1347
1348 /* Is somebody trying to be evil! */
1349 if (c < 0x80 ||
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))
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;
1389 }
1390 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1391 else if(term->sco_acs &&
1392 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1393 {
1394 if (term->sco_acs == 2) c |= 0x80;
1395 c |= ATTR_SCOACS;
1396 } else {
1397 switch (term->cset_attr[term->cset]) {
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:
1405 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1406 c = term->ucsdata->unitab_ctrl[c];
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:
1418 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1419 c = term->ucsdata->unitab_ctrl[c];
1420 else
1421 c = ((unsigned char) c) | ATTR_ASCII;
1422 break;
1423 case ATTR_SCOACS:
1424 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1425 break;
1426 }
1427 }
1428 }
1429
1430 /* How about C1 controls ? */
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;
1435 c = '@' + (c & 0x1F);
1436 }
1437
1438 /* Or the GL control. */
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;
1443 fix_cpos;
1444 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1445 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1446 } else
1447 /* Or normal C0 controls. */
1448 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1449 switch (c) {
1450 case '\005': /* terminal type query */
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 */
1459 compatibility(ANSIMIN);
1460 if (term->ldisc) {
1461 char abuf[256], *s, *d;
1462 int state = 0;
1463 for (s = term->cfg.answerback, d = abuf; *s; s++) {
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
1476 *d++ = *s;
1477 }
1478 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1479 abuf, d - abuf, 0);
1480 }
1481 break;
1482 case '\007':
1483 {
1484 struct beeptime *newbeep;
1485 unsigned long ticks;
1486
1487 ticks = GETTICKCOUNT();
1488
1489 if (!term->beep_overloaded) {
1490 newbeep = smalloc(sizeof(struct beeptime));
1491 newbeep->ticks = ticks;
1492 newbeep->next = NULL;
1493 if (!term->beephead)
1494 term->beephead = newbeep;
1495 else
1496 term->beeptail->next = newbeep;
1497 term->beeptail = newbeep;
1498 term->nbeeps++;
1499 }
1500
1501 /*
1502 * Throw out any beeps that happened more than
1503 * t seconds ago.
1504 */
1505 while (term->beephead &&
1506 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1507 struct beeptime *tmp = term->beephead;
1508 term->beephead = tmp->next;
1509 sfree(tmp);
1510 if (!term->beephead)
1511 term->beeptail = NULL;
1512 term->nbeeps--;
1513 }
1514
1515 if (term->cfg.bellovl && term->beep_overloaded &&
1516 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1517 /*
1518 * If we're currently overloaded and the
1519 * last beep was more than s seconds ago,
1520 * leave overload mode.
1521 */
1522 term->beep_overloaded = FALSE;
1523 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1524 term->nbeeps >= term->cfg.bellovl_n) {
1525 /*
1526 * Now, if we have n or more beeps
1527 * remaining in the queue, go into overload
1528 * mode.
1529 */
1530 term->beep_overloaded = TRUE;
1531 }
1532 term->lastbeep = ticks;
1533
1534 /*
1535 * Perform an actual beep if we're not overloaded.
1536 */
1537 if (!term->cfg.bellovl || !term->beep_overloaded) {
1538 beep(term->frontend, term->cfg.beep);
1539 if (term->cfg.beep == BELL_VISUAL) {
1540 term->in_vbell = TRUE;
1541 term->vbell_startpoint = ticks;
1542 term_update(term);
1543 }
1544 }
1545 term->disptop = 0;
1546 }
1547 break;
1548 case '\b':
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;
1556 else
1557 term->curs.x--;
1558 fix_cpos;
1559 term->seen_disp_event = TRUE;
1560 break;
1561 case '\016':
1562 compatibility(VT100);
1563 term->cset = 1;
1564 break;
1565 case '\017':
1566 compatibility(VT100);
1567 term->cset = 0;
1568 break;
1569 case '\033':
1570 if (term->vt52_mode)
1571 term->termstate = VT52_ESC;
1572 else {
1573 compatibility(ANSIMIN);
1574 term->termstate = SEEN_ESC;
1575 term->esc_query = FALSE;
1576 }
1577 break;
1578 case '\015':
1579 term->curs.x = 0;
1580 term->wrapnext = FALSE;
1581 fix_cpos;
1582 term->seen_disp_event = TRUE;
1583 term->paste_hold = 0;
1584 if (term->logctx)
1585 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1586 break;
1587 case '\014':
1588 if (has_compat(SCOANSI)) {
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;
1594 break;
1595 }
1596 case '\013':
1597 compatibility(VT100);
1598 case '\012':
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++;
1603 if (term->cfg.lfhascr)
1604 term->curs.x = 0;
1605 fix_cpos;
1606 term->wrapnext = FALSE;
1607 term->seen_disp_event = 1;
1608 term->paste_hold = 0;
1609 if (term->logctx)
1610 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1611 break;
1612 case '\t':
1613 {
1614 pos old_curs = term->curs;
1615 unsigned long *ldata = lineptr(term->curs.y);
1616
1617 do {
1618 term->curs.x++;
1619 } while (term->curs.x < term->cols - 1 &&
1620 !term->tabs[term->curs.x]);
1621
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;
1625 } else {
1626 if (term->curs.x >= term->cols)
1627 term->curs.x = term->cols - 1;
1628 }
1629
1630 fix_cpos;
1631 check_selection(term, old_curs, term->curs);
1632 }
1633 term->seen_disp_event = TRUE;
1634 break;
1635 }
1636 } else
1637 switch (term->termstate) {
1638 case TOPLEVEL:
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;
1648 fix_cpos;
1649 term->wrapnext = FALSE;
1650 }
1651 if (term->insert)
1652 insch(term, 1);
1653 if (term->selstate != NO_SELECTION) {
1654 pos cursplus = term->curs;
1655 incpos(cursplus);
1656 check_selection(term, term->curs, cursplus);
1657 }
1658 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1659 term->logctx)
1660 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1661 {
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:
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;
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;
1699 fix_cpos;
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);
1703 }
1704 *term->cpos++ = c | term->curr_attr;
1705 *term->cpos++ = UCSWIDE | term->curr_attr;
1706 term->curs.x++;
1707 break;
1708 case 1:
1709 check_boundary(term, term->curs.x, term->curs.y);
1710 check_boundary(term, term->curs.x+1, term->curs.y);
1711 *term->cpos++ = c | term->curr_attr;
1712 break;
1713 default:
1714 continue;
1715 }
1716 }
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;
1729 fix_cpos;
1730 term->wrapnext = FALSE;
1731 }
1732 }
1733 term->seen_disp_event = 1;
1734 break;
1735
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 == '\\') {
1743 do_osc(term);
1744 term->termstate = TOPLEVEL;
1745 break;
1746 }
1747 /* else fall through */
1748 case SEEN_ESC:
1749 if (c >= ' ' && c <= '/') {
1750 if (term->esc_query)
1751 term->esc_query = -1;
1752 else
1753 term->esc_query = c;
1754 break;
1755 }
1756 term->termstate = TOPLEVEL;
1757 switch (ANSI(c, term->esc_query)) {
1758 case '[': /* enter CSI mode */
1759 term->termstate = SEEN_CSI;
1760 term->esc_nargs = 1;
1761 term->esc_args[0] = ARG_DEFAULT;
1762 term->esc_query = FALSE;
1763 break;
1764 case ']': /* xterm escape sequences */
1765 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1766 compatibility(OTHER);
1767 term->termstate = SEEN_OSC;
1768 term->esc_args[0] = 0;
1769 break;
1770 case '7': /* save cursor */
1771 compatibility(VT100);
1772 save_cursor(term, TRUE);
1773 break;
1774 case '8': /* restore cursor */
1775 compatibility(VT100);
1776 save_cursor(term, FALSE);
1777 term->seen_disp_event = TRUE;
1778 break;
1779 case '=':
1780 compatibility(VT100);
1781 term->app_keypad_keys = TRUE;
1782 break;
1783 case '>':
1784 compatibility(VT100);
1785 term->app_keypad_keys = FALSE;
1786 break;
1787 case 'D': /* exactly equivalent to LF */
1788 compatibility(VT100);
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++;
1793 fix_cpos;
1794 term->wrapnext = FALSE;
1795 term->seen_disp_event = TRUE;
1796 break;
1797 case 'E': /* exactly equivalent to CR-LF */
1798 compatibility(VT100);
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++;
1804 fix_cpos;
1805 term->wrapnext = FALSE;
1806 term->seen_disp_event = TRUE;
1807 break;
1808 case 'M': /* reverse index - backwards LF */
1809 compatibility(VT100);
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--;
1814 fix_cpos;
1815 term->wrapnext = FALSE;
1816 term->seen_disp_event = TRUE;
1817 break;
1818 case 'Z': /* terminal type query */
1819 compatibility(VT100);
1820 if (term->ldisc)
1821 ldisc_send(term->ldisc, term->id_string,
1822 strlen(term->id_string), 0);
1823 break;
1824 case 'c': /* restore power-on settings */
1825 compatibility(VT100);
1826 power_on(term);
1827 if (term->ldisc) /* cause ldisc to notice changes */
1828 ldisc_send(term->ldisc, NULL, 0, 0);
1829 if (term->reset_132) {
1830 if (!term->cfg.no_remote_resize)
1831 request_resize(term->frontend, 80, term->rows);
1832 term->reset_132 = 0;
1833 }
1834 fix_cpos;
1835 term->disptop = 0;
1836 term->seen_disp_event = TRUE;
1837 break;
1838 case 'H': /* set a tab */
1839 compatibility(VT100);
1840 term->tabs[term->curs.x] = TRUE;
1841 break;
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
1850 for (i = 0; i < term->rows; i++) {
1851 ldata = lineptr(i);
1852 for (j = 0; j < term->cols; j++)
1853 ldata[j] = ATTR_DEFAULT | 'E';
1854 ldata[term->cols] = 0;
1855 }
1856 term->disptop = 0;
1857 term->seen_disp_event = TRUE;
1858 scrtop.x = scrtop.y = 0;
1859 scrbot.x = 0;
1860 scrbot.y = term->rows;
1861 check_selection(term, scrtop, scrbot);
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;
1873 switch (ANSI(c, term->esc_query)) {
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;
1883 default: /* spiritually case ANSI('6', '#'): */
1884 nlattr = LATTR_WIDE;
1885 break;
1886 }
1887 ldata = lineptr(term->curs.y);
1888 ldata[term->cols] &= ~LATTR_MODE;
1889 ldata[term->cols] |= nlattr;
1890 }
1891 break;
1892
1893 case ANSI('A', '('):
1894 compatibility(VT100);
1895 if (!term->cfg.no_remote_charset)
1896 term->cset_attr[0] = ATTR_GBCHR;
1897 break;
1898 case ANSI('B', '('):
1899 compatibility(VT100);
1900 if (!term->cfg.no_remote_charset)
1901 term->cset_attr[0] = ATTR_ASCII;
1902 break;
1903 case ANSI('0', '('):
1904 compatibility(VT100);
1905 if (!term->cfg.no_remote_charset)
1906 term->cset_attr[0] = ATTR_LINEDRW;
1907 break;
1908 case ANSI('U', '('):
1909 compatibility(OTHER);
1910 if (!term->cfg.no_remote_charset)
1911 term->cset_attr[0] = ATTR_SCOACS;
1912 break;
1913
1914 case ANSI('A', ')'):
1915 compatibility(VT100);
1916 if (!term->cfg.no_remote_charset)
1917 term->cset_attr[1] = ATTR_GBCHR;
1918 break;
1919 case ANSI('B', ')'):
1920 compatibility(VT100);
1921 if (!term->cfg.no_remote_charset)
1922 term->cset_attr[1] = ATTR_ASCII;
1923 break;
1924 case ANSI('0', ')'):
1925 compatibility(VT100);
1926 if (!term->cfg.no_remote_charset)
1927 term->cset_attr[1] = ATTR_LINEDRW;
1928 break;
1929 case ANSI('U', ')'):
1930 compatibility(OTHER);
1931 if (!term->cfg.no_remote_charset)
1932 term->cset_attr[1] = ATTR_SCOACS;
1933 break;
1934
1935 case ANSI('8', '%'): /* Old Linux code */
1936 case ANSI('G', '%'):
1937 compatibility(OTHER);
1938 if (!term->cfg.no_remote_charset)
1939 term->utf = 1;
1940 break;
1941 case ANSI('@', '%'):
1942 compatibility(OTHER);
1943 if (!term->cfg.no_remote_charset)
1944 term->utf = 0;
1945 break;
1946 }
1947 break;
1948 case SEEN_CSI:
1949 term->termstate = TOPLEVEL; /* default */
1950 if (isdigit(c)) {
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';
1956 }
1957 term->termstate = SEEN_CSI;
1958 } else if (c == ';') {
1959 if (++term->esc_nargs <= ARGS_MAX)
1960 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1961 term->termstate = SEEN_CSI;
1962 } else if (c < '@') {
1963 if (term->esc_query)
1964 term->esc_query = -1;
1965 else if (c == '?')
1966 term->esc_query = TRUE;
1967 else
1968 term->esc_query = c;
1969 term->termstate = SEEN_CSI;
1970 } else
1971 switch (ANSI(c, term->esc_query)) {
1972 case 'A': /* move up N lines */
1973 move(term, term->curs.x,
1974 term->curs.y - def(term->esc_args[0], 1), 1);
1975 term->seen_disp_event = TRUE;
1976 break;
1977 case 'e': /* move down N lines */
1978 compatibility(ANSI);
1979 /* FALLTHROUGH */
1980 case 'B':
1981 move(term, term->curs.x,
1982 term->curs.y + def(term->esc_args[0], 1), 1);
1983 term->seen_disp_event = TRUE;
1984 break;
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 */
1989 if (term->ldisc)
1990 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1991 break;
1992 case 'a': /* move right N cols */
1993 compatibility(ANSI);
1994 /* FALLTHROUGH */
1995 case 'C':
1996 move(term, term->curs.x + def(term->esc_args[0], 1),
1997 term->curs.y, 1);
1998 term->seen_disp_event = TRUE;
1999 break;
2000 case 'D': /* move left N cols */
2001 move(term, term->curs.x - def(term->esc_args[0], 1),
2002 term->curs.y, 1);
2003 term->seen_disp_event = TRUE;
2004 break;
2005 case 'E': /* move down N lines and CR */
2006 compatibility(ANSI);
2007 move(term, 0,
2008 term->curs.y + def(term->esc_args[0], 1), 1);
2009 term->seen_disp_event = TRUE;
2010 break;
2011 case 'F': /* move up N lines and CR */
2012 compatibility(ANSI);
2013 move(term, 0,
2014 term->curs.y - def(term->esc_args[0], 1), 1);
2015 term->seen_disp_event = TRUE;
2016 break;
2017 case 'G':
2018 case '`': /* set horizontal posn */
2019 compatibility(ANSI);
2020 move(term, def(term->esc_args[0], 1) - 1,
2021 term->curs.y, 0);
2022 term->seen_disp_event = TRUE;
2023 break;
2024 case 'd': /* set vertical posn */
2025 compatibility(ANSI);
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;
2031 break;
2032 case 'H':
2033 case 'f': /* set horz and vert posns at once */
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;
2041 break;
2042 case 'J': /* erase screen or parts of it */
2043 {
2044 unsigned int i = def(term->esc_args[0], 0) + 1;
2045 if (i > 3)
2046 i = 0;
2047 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2048 }
2049 term->disptop = 0;
2050 term->seen_disp_event = TRUE;
2051 break;
2052 case 'K': /* erase line or parts of it */
2053 {
2054 unsigned int i = def(term->esc_args[0], 0) + 1;
2055 if (i > 3)
2056 i = 0;
2057 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2058 }
2059 term->seen_disp_event = TRUE;
2060 break;
2061 case 'L': /* insert lines */
2062 compatibility(VT102);
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);
2066 fix_cpos;
2067 term->seen_disp_event = TRUE;
2068 break;
2069 case 'M': /* delete lines */
2070 compatibility(VT102);
2071 if (term->curs.y <= term->marg_b)
2072 scroll(term, term->curs.y, term->marg_b,
2073 def(term->esc_args[0], 1),
2074 TRUE);
2075 fix_cpos;
2076 term->seen_disp_event = TRUE;
2077 break;
2078 case '@': /* insert chars */
2079 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2080 compatibility(VT102);
2081 insch(term, def(term->esc_args[0], 1));
2082 term->seen_disp_event = TRUE;
2083 break;
2084 case 'P': /* delete chars */
2085 compatibility(VT102);
2086 insch(term, -def(term->esc_args[0], 1));
2087 term->seen_disp_event = TRUE;
2088 break;
2089 case 'c': /* terminal type query */
2090 compatibility(VT100);
2091 /* This is the response for a VT102 */
2092 if (term->ldisc)
2093 ldisc_send(term->ldisc, term->id_string,
2094 strlen(term->id_string), 0);
2095 break;
2096 case 'n': /* cursor position query */
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 }
2106 }
2107 break;
2108 case 'h': /* toggle modes to high */
2109 case ANSI_QUE('h'):
2110 compatibility(VT100);
2111 {
2112 int i;
2113 for (i = 0; i < term->esc_nargs; i++)
2114 toggle_mode(term, term->esc_args[i],
2115 term->esc_query, TRUE);
2116 }
2117 break;
2118 case 'i':
2119 case ANSI_QUE('i'):
2120 compatibility(VT100);
2121 {
2122 if (term->esc_nargs != 1) break;
2123 if (term->esc_args[0] == 5 && *term->cfg.printer) {
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);
2131 }
2132 }
2133 break;
2134 case 'l': /* toggle modes to low */
2135 case ANSI_QUE('l'):
2136 compatibility(VT100);
2137 {
2138 int i;
2139 for (i = 0; i < term->esc_nargs; i++)
2140 toggle_mode(term, term->esc_args[i],
2141 term->esc_query, FALSE);
2142 }
2143 break;
2144 case 'g': /* clear tabs */
2145 compatibility(VT100);
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) {
2150 int i;
2151 for (i = 0; i < term->cols; i++)
2152 term->tabs[i] = FALSE;
2153 }
2154 }
2155 break;
2156 case 'r': /* set scroll margins */
2157 compatibility(VT100);
2158 if (term->esc_nargs <= 2) {
2159 int top, bot;
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;
2167 /* VTTEST Bug 9 - if region is less than 2 lines
2168 * don't change region.
2169 */
2170 if (bot - top > 0) {
2171 term->marg_t = top;
2172 term->marg_b = bot;
2173 term->curs.x = 0;
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 *
2180 * Well actually it should for
2181 * Origin mode - RDB
2182 */
2183 term->curs.y = (term->dec_om ?
2184 term->marg_t : 0);
2185 fix_cpos;
2186 term->seen_disp_event = TRUE;
2187 }
2188 }
2189 break;
2190 case 'm': /* set graphics rendition */
2191 {
2192 /*
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.
2198 *
2199 * case 2:
2200 * This is sometimes DIM, eg on the
2201 * GIGI and Linux
2202 * case 8:
2203 * This is sometimes INVIS various ANSI.
2204 * case 21:
2205 * This like 22 disables BOLD, DIM and INVIS
2206 *
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.
2216 *
2217 * The 39 and 49 attributes are likely
2218 * to be unimplemented.
2219 */
2220 int i;
2221 for (i = 0; i < term->esc_nargs; i++) {
2222 switch (def(term->esc_args[i], 0)) {
2223 case 0: /* restore defaults */
2224 term->curr_attr = ATTR_DEFAULT;
2225 break;
2226 case 1: /* enable bold */
2227 compatibility(VT100AVO);
2228 term->curr_attr |= ATTR_BOLD;
2229 break;
2230 case 21: /* (enable double underline) */
2231 compatibility(OTHER);
2232 case 4: /* enable underline */
2233 compatibility(VT100AVO);
2234 term->curr_attr |= ATTR_UNDER;
2235 break;
2236 case 5: /* enable blink */
2237 compatibility(VT100AVO);
2238 term->curr_attr |= ATTR_BLINK;
2239 break;
2240 case 7: /* enable reverse video */
2241 term->curr_attr |= ATTR_REVERSE;
2242 break;
2243 case 10: /* SCO acs off */
2244 compatibility(SCOANSI);
2245 if (term->cfg.no_remote_charset) break;
2246 term->sco_acs = 0; break;
2247 case 11: /* SCO acs on */
2248 compatibility(SCOANSI);
2249 if (term->cfg.no_remote_charset) break;
2250 term->sco_acs = 1; break;
2251 case 12: /* SCO acs on, |0x80 */
2252 compatibility(SCOANSI);
2253 if (term->cfg.no_remote_charset) break;
2254 term->sco_acs = 2; break;
2255 case 22: /* disable bold */
2256 compatibility2(OTHER, VT220);
2257 term->curr_attr &= ~ATTR_BOLD;
2258 break;
2259 case 24: /* disable underline */
2260 compatibility2(OTHER, VT220);
2261 term->curr_attr &= ~ATTR_UNDER;
2262 break;
2263 case 25: /* disable blink */
2264 compatibility2(OTHER, VT220);
2265 term->curr_attr &= ~ATTR_BLINK;
2266 break;
2267 case 27: /* disable reverse video */
2268 compatibility2(OTHER, VT220);
2269 term->curr_attr &= ~ATTR_REVERSE;
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 */
2280 term->curr_attr &= ~ATTR_FGMASK;
2281 term->curr_attr |=
2282 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2283 break;
2284 case 39: /* default-foreground */
2285 term->curr_attr &= ~ATTR_FGMASK;
2286 term->curr_attr |= ATTR_DEFFG;
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 */
2297 term->curr_attr &= ~ATTR_BGMASK;
2298 term->curr_attr |=
2299 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2300 break;
2301 case 49: /* default-background */
2302 term->curr_attr &= ~ATTR_BGMASK;
2303 term->curr_attr |= ATTR_DEFBG;
2304 break;
2305 }
2306 }
2307 if (term->use_bce)
2308 term->erase_char = (' ' | ATTR_ASCII |
2309 (term->curr_attr &
2310 (ATTR_FGMASK |
2311 ATTR_BGMASK)));
2312 }
2313 break;
2314 case 's': /* save cursor */
2315 save_cursor(term, TRUE);
2316 break;
2317 case 'u': /* restore cursor */
2318 save_cursor(term, FALSE);
2319 term->seen_disp_event = TRUE;
2320 break;
2321 case 't': /* set page size - ie window height */
2322 /*
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 */
2328 if (term->esc_nargs <= 1
2329 && (term->esc_args[0] < 1 ||
2330 term->esc_args[0] >= 24)) {
2331 compatibility(VT340TEXT);
2332 if (!term->cfg.no_remote_resize)
2333 request_resize(term->frontend, term->cols,
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) {
2339 compatibility(OTHER);
2340
2341 switch (term->esc_args[0]) {
2342 int x, y, len;
2343 char buf[80], *p;
2344 case 1:
2345 set_iconic(term->frontend, FALSE);
2346 break;
2347 case 2:
2348 set_iconic(term->frontend, TRUE);
2349 break;
2350 case 3:
2351 if (term->esc_nargs >= 3) {
2352 if (!term->cfg.no_remote_resize)
2353 move_window(term->frontend,
2354 def(term->esc_args[1], 0),
2355 def(term->esc_args[2], 0));
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:
2365 /* move to top */
2366 set_zorder(term->frontend, TRUE);
2367 break;
2368 case 6:
2369 /* move to bottom */
2370 set_zorder(term->frontend, FALSE);
2371 break;
2372 case 7:
2373 refresh_window(term->frontend);
2374 break;
2375 case 8:
2376 if (term->esc_nargs >= 3) {
2377 if (!term->cfg.no_remote_resize)
2378 request_resize(term->frontend,
2379 def(term->esc_args[2], term->cfg.width),
2380 def(term->esc_args[1], term->cfg.height));
2381 }
2382 break;
2383 case 9:
2384 if (term->esc_nargs >= 2)
2385 set_zoomed(term->frontend,
2386 term->esc_args[1] ?
2387 TRUE : FALSE);
2388 break;
2389 case 11:
2390 if (term->ldisc)
2391 ldisc_send(term->ldisc,
2392 is_iconic(term->frontend) ?
2393 "\033[1t" : "\033[2t", 4, 0);
2394 break;
2395 case 13:
2396 if (term->ldisc) {
2397 get_window_pos(term->frontend, &x, &y);
2398 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2399 ldisc_send(term->ldisc, buf, len, 0);
2400 }
2401 break;
2402 case 14:
2403 if (term->ldisc) {
2404 get_window_pixels(term->frontend, &x, &y);
2405 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2406 ldisc_send(term->ldisc, buf, len, 0);
2407 }
2408 break;
2409 case 18:
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 }
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:
2434 if (term->ldisc) {
2435 p = get_window_title(term->frontend, TRUE);
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 }
2441 break;
2442 case 21:
2443 if (term->ldisc) {
2444 p = get_window_title(term->frontend,FALSE);
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 }
2450 break;
2451 }
2452 }
2453 break;
2454 case 'S':
2455 compatibility(SCOANSI);
2456 scroll(term, term->marg_t, term->marg_b,
2457 def(term->esc_args[0], 1), TRUE);
2458 fix_cpos;
2459 term->wrapnext = FALSE;
2460 term->seen_disp_event = TRUE;
2461 break;
2462 case 'T':
2463 compatibility(SCOANSI);
2464 scroll(term, term->marg_t, term->marg_b,
2465 -def(term->esc_args[0], 1), TRUE);
2466 fix_cpos;
2467 term->wrapnext = FALSE;
2468 term->seen_disp_event = TRUE;
2469 break;
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);
2477 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2478 if (!term->cfg.no_remote_resize)
2479 request_resize(term->frontend, term->cols,
2480 def(term->esc_args[0],
2481 term->cfg.height));
2482 deselect(term);
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);
2491 if (term->esc_nargs <= 1) {
2492 if (!term->cfg.no_remote_resize)
2493 request_resize(term->frontend,
2494 def(term->esc_args[0],
2495 term->cfg.width), term->rows);
2496 deselect(term);
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 {
2503 int n = def(term->esc_args[0], 1);
2504 pos cursplus;
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;
2509 cursplus.x += n;
2510 check_boundary(term, term->curs.x, term->curs.y);
2511 check_boundary(term, term->curs.x+n, term->curs.y);
2512 check_selection(term, term->curs, cursplus);
2513 while (n--)
2514 *p++ = term->erase_char;
2515 term->seen_disp_event = TRUE;
2516 }
2517 break;
2518 case 'x': /* report terminal characteristics */
2519 compatibility(VT100);
2520 if (term->ldisc) {
2521 char buf[32];
2522 int i = def(term->esc_args[0], 0);
2523 if (i == 0 || i == 1) {
2524 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2525 buf[2] += i;
2526 ldisc_send(term->ldisc, buf, 20, 0);
2527 }
2528 }
2529 break;
2530 case 'Z': /* BackTab for xterm */
2531 compatibility(OTHER);
2532 {
2533 int i = def(term->esc_args[0], 1);
2534 pos old_curs = term->curs;
2535
2536 for(;i>0 && term->curs.x>0; i--) {
2537 do {
2538 term->curs.x--;
2539 } while (term->curs.x >0 &&
2540 !term->tabs[term->curs.x]);
2541 }
2542 fix_cpos;
2543 check_selection(term, old_curs, term->curs);
2544 }
2545 break;
2546 case ANSI('L', '='):
2547 compatibility(OTHER);
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)));
2554 break;
2555 case ANSI('E', '='):
2556 compatibility(OTHER);
2557 term->blink_is_real = (term->esc_args[0] >= 1);
2558 break;
2559 case ANSI('p', '"'):
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.
2565 *
2566 * The arg in 40..42,50 are a PuTTY extension.
2567 * The 2nd arg, 8bit vs 7bit is not checked.
2568 *
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.
2574 *
2575 * Note ESC c will NOT change this!
2576 */
2577
2578 switch (term->esc_args[0]) {
2579 case 61:
2580 term->compatibility_level &= ~TM_VTXXX;
2581 term->compatibility_level |= TM_VT102;
2582 break;
2583 case 62:
2584 term->compatibility_level &= ~TM_VTXXX;
2585 term->compatibility_level |= TM_VT220;
2586 break;
2587
2588 default:
2589 if (term->esc_args[0] > 60 &&
2590 term->esc_args[0] < 70)
2591 term->compatibility_level |= TM_VTXXX;
2592 break;
2593
2594 case 40:
2595 term->compatibility_level &= TM_VTXXX;
2596 break;
2597 case 41:
2598 term->compatibility_level = TM_PUTTY;
2599 break;
2600 case 42:
2601 term->compatibility_level = TM_SCOANSI;
2602 break;
2603
2604 case ARG_DEFAULT:
2605 term->compatibility_level = TM_PUTTY;
2606 break;
2607 case 50:
2608 break;
2609 }
2610
2611 /* Change the response to CSI c */
2612 if (term->esc_args[0] == 50) {
2613 int i;
2614 char lbuf[64];
2615 strcpy(term->id_string, "\033[?");
2616 for (i = 1; i < term->esc_nargs; i++) {
2617 if (i != 1)
2618 strcat(term->id_string, ";");
2619 sprintf(lbuf, "%d", term->esc_args[i]);
2620 strcat(term->id_string, lbuf);
2621 }
2622 strcat(term->id_string, "c");
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)) {
2629 if (!term->cfg.no_remote_resize) {
2630 if (term->reset_132)
2631 request_resize(132, 24);
2632 else
2633 request_resize(80, 24);
2634 }
2635 }
2636 #endif
2637 break;
2638 }
2639 break;
2640 case SEEN_OSC:
2641 term->osc_w = FALSE;
2642 switch (c) {
2643 case 'P': /* Linux palette sequence */
2644 term->termstate = SEEN_OSC_P;
2645 term->osc_strlen = 0;
2646 break;
2647 case 'R': /* Linux palette reset */
2648 palette_reset(term->frontend);
2649 term_invalidate(term);
2650 term->termstate = TOPLEVEL;
2651 break;
2652 case 'W': /* word-set */
2653 term->termstate = SEEN_OSC_W;
2654 term->osc_w = TRUE;
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':
2666 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2667 break;
2668 case 'L':
2669 /*
2670 * Grotty hack to support xterm and DECterm title
2671 * sequences concurrently.
2672 */
2673 if (term->esc_args[0] == 2) {
2674 term->esc_args[0] = 1;
2675 break;
2676 }
2677 /* else fall through */
2678 default:
2679 term->termstate = OSC_STRING;
2680 term->osc_strlen = 0;
2681 }
2682 break;
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
2693 */
2694 if (c == '\012' || c == '\015') {
2695 term->termstate = TOPLEVEL;
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 */
2704 do_osc(term);
2705 term->termstate = TOPLEVEL;
2706 } else if (c == '\033')
2707 term->termstate = OSC_MAYBE_ST;
2708 else if (term->osc_strlen < OSC_STR_MAX)
2709 term->osc_string[term->osc_strlen++] = c;
2710 break;
2711 case SEEN_OSC_P:
2712 {
2713 int max = (term->osc_strlen == 0 ? 21 : 16);
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;
2721 else {
2722 term->termstate = TOPLEVEL;
2723 break;
2724 }
2725 term->osc_string[term->osc_strlen++] = val;
2726 if (term->osc_strlen >= 7) {
2727 palette_set(term->frontend, term->osc_string[0],
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;
2733 }
2734 }
2735 break;
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':
2748 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2749 break;
2750 default:
2751 term->termstate = OSC_STRING;
2752 term->osc_strlen = 0;
2753 }
2754 break;
2755 case VT52_ESC:
2756 term->termstate = TOPLEVEL;
2757 term->seen_disp_event = TRUE;
2758 switch (c) {
2759 case 'A':
2760 move(term, term->curs.x, term->curs.y - 1, 1);
2761 break;
2762 case 'B':
2763 move(term, term->curs.x, term->curs.y + 1, 1);
2764 break;
2765 case 'C':
2766 move(term, term->curs.x + 1, term->curs.y, 1);
2767 break;
2768 case 'D':
2769 move(term, term->curs.x - 1, term->curs.y, 1);
2770 break;
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 */
2811 case 'F':
2812 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2813 break;
2814 case 'G':
2815 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2816 break;
2817 case 'H':
2818 move(term, 0, 0, 0);
2819 break;
2820 case 'I':
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--;
2825 fix_cpos;
2826 term->wrapnext = FALSE;
2827 break;
2828 case 'J':
2829 erase_lots(term, FALSE, FALSE, TRUE);
2830 term->disptop = 0;
2831 break;
2832 case 'K':
2833 erase_lots(term, TRUE, FALSE, TRUE);
2834 break;
2835 #if 0
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;
2845 #endif
2846 case 'Y':
2847 term->termstate = VT52_Y1;
2848 break;
2849 case 'Z':
2850 if (term->ldisc)
2851 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2852 break;
2853 case '=':
2854 term->app_keypad_keys = TRUE;
2855 break;
2856 case '>':
2857 term->app_keypad_keys = FALSE;
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 */
2864 term->vt52_mode = FALSE;
2865 term->blink_is_real = term->cfg.blinktext;
2866 break;
2867 #if 0
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;
2877 #endif
2878
2879 #ifdef VT52_PLUS
2880 case 'E':
2881 /* compatibility(ATARI) */
2882 move(term, 0, 0, 0);
2883 erase_lots(term, FALSE, FALSE, TRUE);
2884 term->disptop = 0;
2885 break;
2886 case 'L':
2887 /* compatibility(ATARI) */
2888 if (term->curs.y <= term->marg_b)
2889 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2890 break;
2891 case 'M':
2892 /* compatibility(ATARI) */
2893 if (term->curs.y <= term->marg_b)
2894 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2895 break;
2896 case 'b':
2897 /* compatibility(ATARI) */
2898 term->termstate = VT52_FG;
2899 break;
2900 case 'c':
2901 /* compatibility(ATARI) */
2902 term->termstate = VT52_BG;
2903 break;
2904 case 'd':
2905 /* compatibility(ATARI) */
2906 erase_lots(term, FALSE, TRUE, FALSE);
2907 term->disptop = 0;
2908 break;
2909 case 'e':
2910 /* compatibility(ATARI) */
2911 term->cursor_on = TRUE;
2912 break;
2913 case 'f':
2914 /* compatibility(ATARI) */
2915 term->cursor_on = FALSE;
2916 break;
2917 /* case 'j': Save cursor position - broken on ST */
2918 /* case 'k': Restore cursor position */
2919 case 'l':
2920 /* compatibility(ATARI) */
2921 erase_lots(term, TRUE, TRUE, TRUE);
2922 term->curs.x = 0;
2923 term->wrapnext = FALSE;
2924 fix_cpos;
2925 break;
2926 case 'o':
2927 /* compatibility(ATARI) */
2928 erase_lots(term, TRUE, TRUE, FALSE);
2929 break;
2930 case 'p':
2931 /* compatibility(ATARI) */
2932 term->curr_attr |= ATTR_REVERSE;
2933 break;
2934 case 'q':
2935 /* compatibility(ATARI) */
2936 term->curr_attr &= ~ATTR_REVERSE;
2937 break;
2938 case 'v': /* wrap Autowrap on - Wyse style */
2939 /* compatibility(ATARI) */
2940 term->wrap = 1;
2941 break;
2942 case 'w': /* Autowrap off */
2943 /* compatibility(ATARI) */
2944 term->wrap = 0;
2945 break;
2946
2947 case 'R':
2948 /* compatibility(OTHER) */
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)));
2955 break;
2956 case 'S':
2957 /* compatibility(VI50) */
2958 term->curr_attr |= ATTR_UNDER;
2959 break;
2960 case 'W':
2961 /* compatibility(VI50) */
2962 term->curr_attr &= ~ATTR_UNDER;
2963 break;
2964 case 'U':
2965 /* compatibility(VI50) */
2966 term->vt52_bold = TRUE;
2967 term->curr_attr |= ATTR_BOLD;
2968 break;
2969 case 'T':
2970 /* compatibility(VI50) */
2971 term->vt52_bold = FALSE;
2972 term->curr_attr &= ~ATTR_BOLD;
2973 break;
2974 #endif
2975 }
2976 break;
2977 case VT52_Y1:
2978 term->termstate = VT52_Y2;
2979 move(term, term->curs.x, c - ' ', 0);
2980 break;
2981 case VT52_Y2:
2982 term->termstate = TOPLEVEL;
2983 move(term, c - ' ', term->curs.y, 0);
2984 break;
2985
2986 #ifdef VT52_PLUS
2987 case VT52_FG:
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)));
2999 break;
3000 case VT52_BG:
3001 term->termstate = TOPLEVEL;
3002 term->curr_attr &= ~ATTR_BGMASK;
3003 term->curr_attr &= ~ATTR_BLINK;
3004 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3005
3006 /* Note: bold background */
3007 if (c & 0x8)
3008 term->curr_attr |= ATTR_BLINK;
3009
3010 if (term->use_bce)
3011 term->erase_char = (' ' | ATTR_ASCII |
3012 (term->curr_attr &
3013 (ATTR_FGMASK | ATTR_BGMASK)));
3014 break;
3015 #endif
3016 default: break; /* placate gcc warning about enum use */
3017 }
3018 if (term->selstate != NO_SELECTION) {
3019 pos cursplus = term->curs;
3020 incpos(cursplus);
3021 check_selection(term, term->curs, cursplus);
3022 }
3023 }
3024
3025 term_print_flush(term);
3026 }
3027
3028 #if 0
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 */
3034 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3035 {
3036 int i, n;
3037
3038 for (i = n = 0; i < term->cols; i++)
3039 n += (*a++ == *b++);
3040 return n;
3041 }
3042 #endif
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 */
3048 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3049 {
3050 int i, j, our_curs_y, our_curs_x;
3051 unsigned long rv, cursor;
3052 pos scrpos;
3053 char ch[1024];
3054 long cursor_background = ERASE_CHAR;
3055 unsigned long ticks;
3056
3057 /*
3058 * Check the visual bell state.
3059 */
3060 if (term->in_vbell) {
3061 ticks = GETTICKCOUNT();
3062 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3063 term->in_vbell = FALSE;
3064 }
3065
3066 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3067
3068 /* Depends on:
3069 * screen array, disptop, scrtop,
3070 * selection, rv,
3071 * cfg.blinkpc, blink_is_real, tblinker,
3072 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3073 */
3074
3075 /* Has the cursor position or type changed ? */
3076 if (term->cursor_on) {
3077 if (term->has_focus) {
3078 if (term->blinker || !term->cfg.blink_cur)
3079 cursor = TATTR_ACTCURS;
3080 else
3081 cursor = 0;
3082 } else
3083 cursor = TATTR_PASCURS;
3084 if (term->wrapnext)
3085 cursor |= TATTR_RIGHTCURS;
3086 } else
3087 cursor = 0;
3088 our_curs_y = term->curs.y - term->disptop;
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 }
3103
3104 if (term->dispcurs && (term->curstype != cursor ||
3105 term->dispcurs !=
3106 term->disptext + our_curs_y * (term->cols + 1) +
3107 our_curs_x)) {
3108 if (term->dispcurs > term->disptext &&
3109 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
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;
3115 }
3116 term->dispcurs = NULL;
3117
3118 /* The normal screen data */
3119 for (i = 0; i < term->rows; i++) {
3120 unsigned long *ldata;
3121 int lattr;
3122 int idx, dirty_line, dirty_run, selected;
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
3129 scrpos.y = i + term->disptop;
3130 ldata = lineptr(scrpos.y);
3131 lattr = (ldata[term->cols] & LATTR_MODE);
3132
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];
3137
3138 for (j = 0; j < term->cols; j++, idx++) {
3139 unsigned long tattr, tchar;
3140 unsigned long *d = ldata + j;
3141 int break_run;
3142 scrpos.x = j;
3143
3144 tchar = (*d & (CHAR_MASK | CSET_MASK));
3145 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3146 switch (tchar & CSET_MASK) {
3147 case ATTR_ASCII:
3148 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3149 break;
3150 case ATTR_LINEDRW:
3151 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3152 break;
3153 case ATTR_SCOACS:
3154 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3155 break;
3156 }
3157 tattr |= (tchar & CSET_MASK);
3158 tchar &= CHAR_MASK;
3159 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3160 tattr |= ATTR_WIDE;
3161
3162 /* Video reversing things */
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;
3172 tattr = (tattr ^ rv
3173 ^ (selected ? ATTR_REVERSE : 0));
3174
3175 /* 'Real' blinking ? */
3176 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3177 if (term->has_focus && term->tblinker) {
3178 tchar = ' ';
3179 tattr &= ~CSET_MASK;
3180 tattr |= ATTR_ACP;
3181 }
3182 tattr &= ~ATTR_BLINK;
3183 }
3184
3185 /*
3186 * Check the font we'll _probably_ be using to see if
3187 * the character is wide when we don't want it to be.
3188 */
3189 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3190 if ((tattr & ATTR_WIDE) == 0 &&
3191 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3192 tattr |= ATTR_NARROW;
3193 } else if (term->disptext[idx]&ATTR_NARROW)
3194 tattr |= ATTR_NARROW;
3195
3196 /* Cursor here ? Save the 'background' */
3197 if (i == our_curs_y && j == our_curs_x) {
3198 cursor_background = tattr | tchar;
3199 term->dispcurs = term->disptext + idx;
3200 }
3201
3202 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3203 dirty_line = TRUE;
3204
3205 break_run = (((tattr ^ attr) & term->attr_mask) ||
3206 j - start >= sizeof(ch));
3207
3208 /* Special hack for VT100 Linedraw glyphs */
3209 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3210 && tchar <= 0xBD) break_run = TRUE;
3211
3212 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3213 if ((tchar | tattr) == term->disptext[idx])
3214 break_run = TRUE;
3215 else if (!dirty_run && ccount == 1)
3216 break_run = TRUE;
3217 }
3218
3219 if (break_run) {
3220 if ((dirty_run || last_run_dirty) && ccount > 0) {
3221 do_text(ctx, start, i, ch, ccount, attr, lattr);
3222 updated_line = 1;
3223 }
3224 start = j;
3225 ccount = 0;
3226 attr = tattr;
3227 if (term->ucsdata->dbcs_screenfont)
3228 last_run_dirty = dirty_run;
3229 dirty_run = dirty_line;
3230 }
3231
3232 if ((tchar | tattr) != term->disptext[idx])
3233 dirty_run = TRUE;
3234 ch[ccount++] = (char) tchar;
3235 term->disptext[idx] = tchar | tattr;
3236
3237 /* If it's a wide char step along to the next one. */
3238 if (tattr & ATTR_WIDE) {
3239 if (++j < term->cols) {
3240 idx++;
3241 d++;
3242 /*
3243 * By construction above, the cursor should not
3244 * be on the right-hand half of this character.
3245 * Ever.
3246 */
3247 assert(!(i == our_curs_y && j == our_curs_x));
3248 if (term->disptext[idx] != *d)
3249 dirty_run = TRUE;
3250 term->disptext[idx] = *d;
3251 }
3252 }
3253 }
3254 if (dirty_run && ccount > 0) {
3255 do_text(ctx, start, i, ch, ccount, attr, lattr);
3256 updated_line = 1;
3257 }
3258
3259 /* Cursor on this line ? (and changed) */
3260 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3261 ch[0] = (char) (cursor_background & CHAR_MASK);
3262 attr = (cursor_background & ATTR_MASK) | cursor;
3263 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3264 term->curstype = cursor;
3265 }
3266 }
3267 }
3268
3269 /*
3270 * Flick the switch that says if blinking things should be shown or hidden.
3271 */
3272
3273 void term_blink(Terminal *term, int flg)
3274 {
3275 long now, blink_diff;
3276
3277 now = GETTICKCOUNT();
3278 blink_diff = now - term->last_tblink;
3279
3280 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3281 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3282 term->last_tblink = now;
3283 term->tblinker = !term->tblinker;
3284 }
3285
3286 if (flg) {
3287 term->blinker = 1;
3288 term->last_blink = now;
3289 return;
3290 }
3291
3292 blink_diff = now - term->last_blink;
3293
3294 /* Make sure the cursor blinks no faster than system blink rate */
3295 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3296 return;
3297
3298 term->last_blink = now;
3299 term->blinker = !term->blinker;
3300 }
3301
3302 /*
3303 * Invalidate the whole screen so it will be repainted in full.
3304 */
3305 void term_invalidate(Terminal *term)
3306 {
3307 int i;
3308
3309 for (i = 0; i < term->rows * (term->cols + 1); i++)
3310 term->disptext[i] = ATTR_INVALID;
3311 }
3312
3313 /*
3314 * Paint the window in response to a WM_PAINT message.
3315 */
3316 void term_paint(Terminal *term, Context ctx,
3317 int left, int top, int right, int bottom, int immediately)
3318 {
3319 int i, j;
3320 if (left < 0) left = 0;
3321 if (top < 0) top = 0;
3322 if (right >= term->cols) right = term->cols-1;
3323 if (bottom >= term->rows) bottom = term->rows-1;
3324
3325 for (i = top; i <= bottom && i < term->rows; i++) {
3326 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3327 LATTR_MODE) == LATTR_NORM)
3328 for (j = left; j <= right && j < term->cols; j++)
3329 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3330 else
3331 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3332 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3333 }
3334
3335 /* This should happen soon enough, also for some reason it sometimes
3336 * fails to actually do anything when re-sizing ... painting the wrong
3337 * window perhaps ?
3338 */
3339 if (immediately)
3340 do_paint (term, ctx, FALSE);
3341 }
3342
3343 /*
3344 * Attempt to scroll the scrollback. The second parameter gives the
3345 * position we want to scroll to; the first is +1 to denote that
3346 * this position is relative to the beginning of the scrollback, -1
3347 * to denote it is relative to the end, and 0 to denote that it is
3348 * relative to the current position.
3349 */
3350 void term_scroll(Terminal *term, int rel, int where)
3351 {
3352 int sbtop = -count234(term->scrollback);
3353 #ifdef OPTIMISE_SCROLL
3354 int olddisptop = term->disptop;
3355 int shift;
3356 #endif /* OPTIMISE_SCROLL */
3357
3358 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3359 if (term->disptop < sbtop)
3360 term->disptop = sbtop;
3361 if (term->disptop > 0)
3362 term->disptop = 0;
3363 update_sbar(term);
3364 #ifdef OPTIMISE_SCROLL
3365 shift = (term->disptop - olddisptop);
3366 if (shift < term->rows && shift > -term->rows)
3367 scroll_display(term, 0, term->rows - 1, shift);
3368 #endif /* OPTIMISE_SCROLL */
3369 term_update(term);
3370 }
3371
3372 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3373 {
3374 wchar_t *workbuf;
3375 wchar_t *wbptr; /* where next char goes within workbuf */
3376 int old_top_x;
3377 int wblen = 0; /* workbuf len */
3378 int buflen; /* amount of memory allocated to workbuf */
3379
3380 buflen = 5120; /* Default size */
3381 workbuf = smalloc(buflen * sizeof(wchar_t));
3382 wbptr = workbuf; /* start filling here */
3383 old_top_x = top.x; /* needed for rect==1 */
3384
3385 while (poslt(top, bottom)) {
3386 int nl = FALSE;
3387 unsigned long *ldata = lineptr(top.y);
3388 pos nlpos;
3389
3390 /*
3391 * nlpos will point at the maximum position on this line we
3392 * should copy up to. So we start it at the end of the
3393 * line...
3394 */
3395 nlpos.y = top.y;
3396 nlpos.x = term->cols;
3397
3398 /*
3399 * ... move it backwards if there's unused space at the end
3400 * of the line (and also set `nl' if this is the case,
3401 * because in normal selection mode this means we need a
3402 * newline at the end)...
3403 */
3404 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3405 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3406 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3407 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3408 && poslt(top, nlpos))
3409 decpos(nlpos);
3410 if (poslt(nlpos, bottom))
3411 nl = TRUE;
3412 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3413 /* Ignore the last char on the line in a WRAPPED2 line. */
3414 decpos(nlpos);
3415 }
3416
3417 /*
3418 * ... and then clip it to the terminal x coordinate if
3419 * we're doing rectangular selection. (In this case we
3420 * still did the above, so that copying e.g. the right-hand
3421 * column from a table doesn't fill with spaces on the
3422 * right.)
3423 */
3424 if (rect) {
3425 if (nlpos.x > bottom.x)
3426 nlpos.x = bottom.x;
3427 nl = (top.y < bottom.y);
3428 }
3429
3430 while (poslt(top, bottom) && poslt(top, nlpos)) {
3431 #if 0
3432 char cbuf[16], *p;
3433 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3434 #else
3435 wchar_t cbuf[16], *p;
3436 int uc = (ldata[top.x] & 0xFFFF);
3437 int set, c;
3438
3439 if (uc == UCSWIDE) {
3440 top.x++;
3441 continue;
3442 }
3443
3444 switch (uc & CSET_MASK) {
3445 case ATTR_LINEDRW:
3446 if (!term->cfg.rawcnp) {
3447 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3448 break;
3449 }
3450 case ATTR_ASCII:
3451 uc = term->ucsdata->unitab_line[uc & 0xFF];
3452 break;
3453 case ATTR_SCOACS:
3454 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3455 break;
3456 }
3457 switch (uc & CSET_MASK) {
3458 case ATTR_ACP:
3459 uc = term->ucsdata->unitab_font[uc & 0xFF];
3460 break;
3461 case ATTR_OEMCP:
3462 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3463 break;
3464 }
3465
3466 set = (uc & CSET_MASK);
3467 c = (uc & CHAR_MASK);
3468 cbuf[0] = uc;
3469 cbuf[1] = 0;
3470
3471 if (DIRECT_FONT(uc)) {
3472 if (c >= ' ' && c != 0x7F) {
3473 char buf[4];
3474 WCHAR wbuf[4];
3475 int rv;
3476 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3477 buf[0] = c;
3478 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3479 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3480 top.x++;
3481 } else {
3482 buf[0] = c;
3483 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3484 }
3485
3486 if (rv > 0) {
3487 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3488 cbuf[rv] = 0;
3489 }
3490 }
3491 }
3492 #endif
3493
3494 for (p = cbuf; *p; p++) {
3495 /* Enough overhead for trailing NL and nul */
3496 if (wblen >= buflen - 16) {
3497 workbuf =
3498 srealloc(workbuf,
3499 sizeof(wchar_t) * (buflen += 100));
3500 wbptr = workbuf + wblen;
3501 }
3502 wblen++;
3503 *wbptr++ = *p;
3504 }
3505 top.x++;
3506 }
3507 if (nl) {
3508 int i;
3509 for (i = 0; i < sel_nl_sz; i++) {
3510 wblen++;
3511 *wbptr++ = sel_nl[i];
3512 }
3513 }
3514 top.y++;
3515 top.x = rect ? old_top_x : 0;
3516 }
3517 #if SELECTION_NUL_TERMINATED
3518 wblen++;
3519 *wbptr++ = 0;
3520 #endif
3521 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3522 if (buflen > 0) /* indicates we allocated this buffer */
3523 sfree(workbuf);
3524 }
3525
3526 void term_copyall(Terminal *term)
3527 {
3528 pos top;
3529 top.y = -count234(term->scrollback);
3530 top.x = 0;
3531 clipme(term, top, term->curs, 0);
3532 }
3533
3534 /*
3535 * The wordness array is mainly for deciding the disposition of the
3536 * US-ASCII characters.
3537 */
3538 static int wordtype(Terminal *term, int uc)
3539 {
3540 struct ucsword {
3541 int start, end, ctype;
3542 };
3543 static const struct ucsword ucs_words[] = {
3544 {
3545 128, 160, 0}, {
3546 161, 191, 1}, {
3547 215, 215, 1}, {
3548 247, 247, 1}, {
3549 0x037e, 0x037e, 1}, /* Greek question mark */
3550 {
3551 0x0387, 0x0387, 1}, /* Greek ano teleia */
3552 {
3553 0x055a, 0x055f, 1}, /* Armenian punctuation */
3554 {
3555 0x0589, 0x0589, 1}, /* Armenian full stop */
3556 {
3557 0x0700, 0x070d, 1}, /* Syriac punctuation */
3558 {
3559 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3560 {
3561 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3562 {
3563 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3564 {
3565 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3566 {
3567 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3568 {
3569 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3570 {
3571 0x2000, 0x200a, 0}, /* Various spaces */
3572 {
3573 0x2070, 0x207f, 2}, /* superscript */
3574 {
3575 0x2080, 0x208f, 2}, /* subscript */
3576 {
3577 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3578 {
3579 0x3000, 0x3000, 0}, /* ideographic space */
3580 {
3581 0x3001, 0x3020, 1}, /* ideographic punctuation */
3582 {
3583 0x303f, 0x309f, 3}, /* Hiragana */
3584 {
3585 0x30a0, 0x30ff, 3}, /* Katakana */
3586 {
3587 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3588 {
3589 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3590 {
3591 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3592 {
3593 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3594 {
3595 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3596 {
3597 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3598 {
3599 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3600 {
3601 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3602 {
3603 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3604 {
3605 0, 0, 0}
3606 };
3607 const struct ucsword *wptr;
3608
3609 uc &= (CSET_MASK | CHAR_MASK);
3610
3611 switch (uc & CSET_MASK) {
3612 case ATTR_LINEDRW:
3613 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3614 break;
3615 case ATTR_ASCII:
3616 uc = term->ucsdata->unitab_line[uc & 0xFF];
3617 break;
3618 case ATTR_SCOACS:
3619 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3620 break;
3621 }
3622 switch (uc & CSET_MASK) {
3623 case ATTR_ACP:
3624 uc = term->ucsdata->unitab_font[uc & 0xFF];
3625 break;
3626 case ATTR_OEMCP:
3627 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3628 break;
3629 }
3630
3631 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3632 * fail as there's such a thing as a double width space. :-(
3633 */
3634 if (term->ucsdata->dbcs_screenfont &&
3635 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3636 return (uc != ' ');
3637
3638 if (uc < 0x80)
3639 return term->wordness[uc];
3640
3641 for (wptr = ucs_words; wptr->start; wptr++) {
3642 if (uc >= wptr->start && uc <= wptr->end)
3643 return wptr->ctype;
3644 }
3645
3646 return 2;
3647 }
3648
3649 /*
3650 * Spread the selection outwards according to the selection mode.
3651 */
3652 static pos sel_spread_half(Terminal *term, pos p, int dir)
3653 {
3654 unsigned long *ldata;
3655 short wvalue;
3656 int topy = -count234(term->scrollback);
3657
3658 ldata = lineptr(p.y);
3659
3660 switch (term->selmode) {
3661 case SM_CHAR:
3662 /*
3663 * In this mode, every character is a separate unit, except
3664 * for runs of spaces at the end of a non-wrapping line.
3665 */
3666 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3667 unsigned long *q = ldata + term->cols;
3668 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3669 q--;
3670 if (q == ldata + term->cols)
3671 q--;
3672 if (p.x >= q - ldata)
3673 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3674 }
3675 break;
3676 case SM_WORD:
3677 /*
3678 * In this mode, the units are maximal runs of characters
3679 * whose `wordness' has the same value.
3680 */
3681 wvalue = wordtype(term, UCSGET(ldata, p.x));
3682 if (dir == +1) {
3683 while (1) {
3684 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3685 term->cols-1 : term->cols);
3686 if (p.x < maxcols-1) {
3687 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3688 p.x++;
3689 else
3690 break;
3691 } else {
3692 if (ldata[term->cols] & LATTR_WRAPPED) {
3693 unsigned long *ldata2;
3694 ldata2 = lineptr(p.y+1);
3695 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3696 p.x = 0;
3697 p.y++;
3698 ldata = ldata2;
3699 } else
3700 break;
3701 } else
3702 break;
3703 }
3704 }
3705 } else {
3706 while (1) {
3707 if (p.x > 0) {
3708 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3709 p.x--;
3710 else
3711 break;
3712 } else {
3713 unsigned long *ldata2;
3714 int maxcols;
3715 if (p.y <= topy)
3716 break;
3717 ldata2 = lineptr(p.y-1);
3718 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3719 term->cols-1 : term->cols);
3720 if (ldata2[term->cols] & LATTR_WRAPPED) {
3721 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3722 == wvalue) {
3723 p.x = maxcols-1;
3724 p.y--;
3725 ldata = ldata2;
3726 } else
3727 break;
3728 } else
3729 break;
3730 }
3731 }
3732 }
3733 break;
3734 case SM_LINE:
3735 /*
3736 * In this mode, every line is a unit.
3737 */
3738 p.x = (dir == -1 ? 0 : term->cols - 1);
3739 break;
3740 }
3741 return p;
3742 }
3743
3744 static void sel_spread(Terminal *term)
3745 {
3746 if (term->seltype == LEXICOGRAPHIC) {
3747 term->selstart = sel_spread_half(term, term->selstart, -1);
3748 decpos(term->selend);
3749 term->selend = sel_spread_half(term, term->selend, +1);
3750 incpos(term->selend);
3751 }
3752 }
3753
3754 void term_do_paste(Terminal *term)
3755 {
3756 wchar_t *data;
3757 int len;
3758
3759 get_clip(term->frontend, &data, &len);
3760 if (data && len > 0) {
3761 wchar_t *p, *q;
3762
3763 term_seen_key_event(term); /* pasted data counts */
3764
3765 if (term->paste_buffer)
3766 sfree(term->paste_buffer);
3767 term->paste_pos = term->paste_hold = term->paste_len = 0;
3768 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3769
3770 p = q = data;
3771 while (p < data + len) {
3772 while (p < data + len &&
3773 !(p <= data + len - sel_nl_sz &&
3774 !memcmp(p, sel_nl, sizeof(sel_nl))))
3775 p++;
3776
3777 {
3778 int i;
3779 for (i = 0; i < p - q; i++) {
3780 term->paste_buffer[term->paste_len++] = q[i];
3781 }
3782 }
3783
3784 if (p <= data + len - sel_nl_sz &&
3785 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3786 term->paste_buffer[term->paste_len++] = '\015';
3787 p += sel_nl_sz;
3788 }
3789 q = p;
3790 }
3791
3792 /* Assume a small paste will be OK in one go. */
3793 if (term->paste_len < 256) {
3794 if (term->ldisc)
3795 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3796 if (term->paste_buffer)
3797 sfree(term->paste_buffer);
3798 term->paste_buffer = 0;
3799 term->paste_pos = term->paste_hold = term->paste_len = 0;
3800 }
3801 }
3802 get_clip(term->frontend, NULL, NULL);
3803 }
3804
3805 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3806 int shift, int ctrl, int alt)
3807 {
3808 pos selpoint;
3809 unsigned long *ldata;
3810 int raw_mouse = (term->xterm_mouse &&
3811 !term->cfg.no_mouse_rep &&
3812 !(term->cfg.mouse_override && shift));
3813 int default_seltype;
3814
3815 if (y < 0) {
3816 y = 0;
3817 if (a == MA_DRAG && !raw_mouse)
3818 term_scroll(term, 0, -1);
3819 }
3820 if (y >= term->rows) {
3821 y = term->rows - 1;
3822 if (a == MA_DRAG && !raw_mouse)
3823 term_scroll(term, 0, +1);
3824 }
3825 if (x < 0) {
3826 if (y > 0) {
3827 x = term->cols - 1;
3828 y--;
3829 } else
3830 x = 0;
3831 }
3832 if (x >= term->cols)
3833 x = term->cols - 1;
3834
3835 selpoint.y = y + term->disptop;
3836 selpoint.x = x;
3837 ldata = lineptr(selpoint.y);
3838 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3839 selpoint.x /= 2;
3840
3841 if (raw_mouse) {
3842 int encstate = 0, r, c;
3843 char abuf[16];
3844
3845 if (term->ldisc) {
3846
3847 switch (b) {
3848 case MBT_LEFT:
3849 encstate = 0x20; /* left button down */
3850 break;
3851 case MBT_MIDDLE:
3852 encstate = 0x21;
3853 break;
3854 case MBT_RIGHT:
3855 encstate = 0x22;
3856 break;
3857 case MBT_WHEEL_UP:
3858 encstate = 0x60;
3859 break;
3860 case MBT_WHEEL_DOWN:
3861 encstate = 0x61;
3862 break;
3863 default: break; /* placate gcc warning about enum use */
3864 }
3865 switch (a) {
3866 case MA_DRAG:
3867 if (term->xterm_mouse == 1)
3868 return;
3869 encstate += 0x20;
3870 break;
3871 case MA_RELEASE:
3872 encstate = 0x23;
3873 term->mouse_is_down = 0;
3874 break;
3875 case MA_CLICK:
3876 if (term->mouse_is_down == b)
3877 return;
3878 term->mouse_is_down = b;
3879 break;
3880 default: break; /* placate gcc warning about enum use */
3881 }
3882 if (shift)
3883 encstate += 0x04;
3884 if (ctrl)
3885 encstate += 0x10;
3886 r = y + 33;
3887 c = x + 33;
3888
3889 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3890 ldisc_send(term->ldisc, abuf, 6, 0);
3891 }
3892 return;
3893 }
3894
3895 b = translate_button(term->frontend, b);
3896
3897 /*
3898 * Set the selection type (rectangular or normal) at the start
3899 * of a selection attempt, from the state of Alt.
3900 */
3901 if (!alt ^ !term->cfg.rect_select)
3902 default_seltype = RECTANGULAR;
3903 else
3904 default_seltype = LEXICOGRAPHIC;
3905
3906 if (term->selstate == NO_SELECTION) {
3907 term->seltype = default_seltype;
3908 }
3909
3910 if (b == MBT_SELECT && a == MA_CLICK) {
3911 deselect(term);
3912 term->selstate = ABOUT_TO;
3913 term->seltype = default_seltype;
3914 term->selanchor = selpoint;
3915 term->selmode = SM_CHAR;
3916 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3917 deselect(term);
3918 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3919 term->selstate = DRAGGING;
3920 term->selstart = term->selanchor = selpoint;
3921 term->selend = term->selstart;
3922 incpos(term->selend);
3923 sel_spread(term);
3924 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3925 (b == MBT_EXTEND && a != MA_RELEASE)) {
3926 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3927 return;
3928 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3929 if (term->seltype == LEXICOGRAPHIC) {
3930 /*
3931 * For normal selection, we extend by moving
3932 * whichever end of the current selection is closer
3933 * to the mouse.
3934 */
3935 if (posdiff(selpoint, term->selstart) <
3936 posdiff(term->selend, term->selstart) / 2) {
3937 term->selanchor = term->selend;
3938 decpos(term->selanchor);
3939 } else {
3940 term->selanchor = term->selstart;
3941 }
3942 } else {
3943 /*
3944 * For rectangular selection, we have a choice of
3945 * _four_ places to put selanchor and selpoint: the
3946 * four corners of the selection.
3947 */
3948 if (2*selpoint.x < term->selstart.x + term->selend.x)
3949 term->selanchor.x = term->selend.x-1;
3950 else
3951 term->selanchor.x = term->selstart.x;
3952
3953 if (2*selpoint.y < term->selstart.y + term->selend.y)
3954 term->selanchor.y = term->selend.y;
3955 else
3956 term->selanchor.y = term->selstart.y;
3957 }
3958 term->selstate = DRAGGING;
3959 }
3960 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3961 term->selanchor = selpoint;
3962 term->selstate = DRAGGING;
3963 if (term->seltype == LEXICOGRAPHIC) {
3964 /*
3965 * For normal selection, we set (selstart,selend) to
3966 * (selpoint,selanchor) in some order.
3967 */
3968 if (poslt(selpoint, term->selanchor)) {
3969 term->selstart = selpoint;
3970 term->selend = term->selanchor;
3971 incpos(term->selend);
3972 } else {
3973 term->selstart = term->selanchor;
3974 term->selend = selpoint;
3975 incpos(term->selend);
3976 }
3977 } else {
3978 /*
3979 * For rectangular selection, we may need to
3980 * interchange x and y coordinates (if the user has
3981 * dragged in the -x and +y directions, or vice versa).
3982 */
3983 term->selstart.x = min(term->selanchor.x, selpoint.x);
3984 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3985 term->selstart.y = min(term->selanchor.y, selpoint.y);
3986 term->selend.y = max(term->selanchor.y, selpoint.y);
3987 }
3988 sel_spread(term);
3989 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3990 if (term->selstate == DRAGGING) {
3991 /*
3992 * We've completed a selection. We now transfer the
3993 * data to the clipboard.
3994 */
3995 clipme(term, term->selstart, term->selend,
3996 (term->seltype == RECTANGULAR));
3997 term->selstate = SELECTED;
3998 } else
3999 term->selstate = NO_SELECTION;
4000 } else if (b == MBT_PASTE
4001 && (a == MA_CLICK
4002 #if MULTICLICK_ONLY_EVENT
4003 || a == MA_2CLK || a == MA_3CLK
4004 #endif
4005 )) {
4006 request_paste(term->frontend);
4007 }
4008
4009 term_update(term);
4010 }
4011
4012 void term_nopaste(Terminal *term)
4013 {
4014 if (term->paste_len == 0)
4015 return;
4016 sfree(term->paste_buffer);
4017 term->paste_buffer = NULL;
4018 term->paste_len = 0;
4019 }
4020
4021 int term_paste_pending(Terminal *term)
4022 {
4023 return term->paste_len != 0;
4024 }
4025
4026 void term_paste(Terminal *term)
4027 {
4028 long now, paste_diff;
4029
4030 if (term->paste_len == 0)
4031 return;
4032
4033 /* Don't wait forever to paste */
4034 if (term->paste_hold) {
4035 now = GETTICKCOUNT();
4036 paste_diff = now - term->last_paste;
4037 if (paste_diff >= 0 && paste_diff < 450)
4038 return;
4039 }
4040 term->paste_hold = 0;
4041
4042 while (term->paste_pos < term->paste_len) {
4043 int n = 0;
4044 while (n + term->paste_pos < term->paste_len) {
4045 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4046 break;
4047 }
4048 if (term->ldisc)
4049 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4050 term->paste_pos += n;
4051
4052 if (term->paste_pos < term->paste_len) {
4053 term->paste_hold = 1;
4054 return;
4055 }
4056 }
4057 sfree(term->paste_buffer);
4058 term->paste_buffer = NULL;
4059 term->paste_len = 0;
4060 }
4061
4062 static void deselect(Terminal *term)
4063 {
4064 term->selstate = NO_SELECTION;
4065 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4066 }
4067
4068 void term_deselect(Terminal *term)
4069 {
4070 deselect(term);
4071 term_update(term);
4072 }
4073
4074 int term_ldisc(Terminal *term, int option)
4075 {
4076 if (option == LD_ECHO)
4077 return term->term_echoing;
4078 if (option == LD_EDIT)
4079 return term->term_editing;
4080 return FALSE;
4081 }
4082
4083 /*
4084 * from_backend(), to get data from the backend for the terminal.
4085 */
4086 int from_backend(void *vterm, int is_stderr, char *data, int len)
4087 {
4088 Terminal *term = (Terminal *)vterm;
4089
4090 assert(len > 0);
4091
4092 bufchain_add(&term->inbuf, data, len);
4093
4094 /*
4095 * term_out() always completely empties inbuf. Therefore,
4096 * there's no reason at all to return anything other than zero
4097 * from this function, because there _can't_ be a question of
4098 * the remote side needing to wait until term_out() has cleared
4099 * a backlog.
4100 *
4101 * This is a slightly suboptimal way to deal with SSH2 - in
4102 * principle, the window mechanism would allow us to continue
4103 * to accept data on forwarded ports and X connections even
4104 * while the terminal processing was going slowly - but we
4105 * can't do the 100% right thing without moving the terminal
4106 * processing into a separate thread, and that might hurt
4107 * portability. So we manage stdout buffering the old SSH1 way:
4108 * if the terminal processing goes slowly, the whole SSH
4109 * connection stops accepting data until it's ready.
4110 *
4111 * In practice, I can't imagine this causing serious trouble.
4112 */
4113 return 0;
4114 }
4115
4116 void term_provide_logctx(Terminal *term, void *logctx)
4117 {
4118 term->logctx = logctx;
4119 }