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