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