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