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