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