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