374330e2 |
1 | #include <windows.h> |
2 | |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
49bad831 |
5 | #include <ctype.h> |
374330e2 |
6 | |
e1c8e0ed |
7 | #include <time.h> |
374330e2 |
8 | #include "putty.h" |
9 | |
c9def1b8 |
10 | #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */ |
e14a5a13 |
11 | #define CL_VT100 0x0002 /* VT100 */ |
12 | #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */ |
13 | #define CL_VT102 0x0008 /* VT102 */ |
14 | #define CL_VT220 0x0010 /* VT220 */ |
15 | #define CL_VT320 0x0020 /* VT320 */ |
16 | #define CL_VT420 0x0040 /* VT420 */ |
17 | #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */ |
18 | #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */ |
c9def1b8 |
19 | #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */ |
20 | #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */ |
21 | #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */ |
e14a5a13 |
22 | |
c9def1b8 |
23 | #define TM_VT100 (CL_ANSIMIN|CL_VT100) |
24 | #define TM_VT100AVO (TM_VT100|CL_VT100AVO) |
25 | #define TM_VT102 (TM_VT100AVO|CL_VT102) |
26 | #define TM_VT220 (TM_VT102|CL_VT220) |
27 | #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320) |
28 | #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI) |
29 | |
30 | #define TM_PUTTY (0xFFFF) |
e14a5a13 |
31 | |
32 | #define compatibility(x) \ |
33 | if ( ((CL_##x)&compatibility_level) == 0 ) { \ |
34 | termstate=TOPLEVEL; \ |
35 | break; \ |
36 | } |
c9def1b8 |
37 | #define compatibility2(x,y) \ |
38 | if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \ |
39 | termstate=TOPLEVEL; \ |
40 | break; \ |
41 | } |
e14a5a13 |
42 | |
43 | #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 ) |
44 | |
45 | static int compatibility_level = TM_PUTTY; |
46 | |
47 | |
374330e2 |
48 | static unsigned long *text; /* buffer of text on terminal screen */ |
49 | static unsigned long *scrtop; /* top of working screen */ |
50 | static unsigned long *disptop; /* top of displayed screen */ |
51 | static unsigned long *sbtop; /* top of scrollback */ |
c9def1b8 |
52 | static unsigned long *sbbot; /* furthest extent of scrollback */ |
374330e2 |
53 | static unsigned long *cpos; /* cursor position (convenience) */ |
54 | static unsigned long *disptext; /* buffer of text on real screen */ |
55 | static unsigned long *wanttext; /* buffer of text we want on screen */ |
56 | static unsigned long *alttext; /* buffer of text on alt. screen */ |
57 | |
156686ef |
58 | #define VBELL_TIMEOUT 100 /* millisecond duration of visual bell */ |
59 | |
60 | struct beeptime { |
61 | struct beeptime *next; |
62 | long ticks; |
63 | }; |
64 | static struct beeptime *beephead, *beeptail; |
65 | int nbeeps; |
66 | int beep_overloaded; |
67 | long lastbeep; |
68 | |
374330e2 |
69 | static unsigned char *selspace; /* buffer for building selections in */ |
70 | |
71 | #define TSIZE (sizeof(*text)) |
72 | #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0) |
73 | |
74 | static unsigned long curr_attr, save_attr; |
e14a5a13 |
75 | static unsigned long erase_char = ERASE_CHAR; |
374330e2 |
76 | |
77 | static int curs_x, curs_y; /* cursor */ |
78 | static int save_x, save_y; /* saved cursor position */ |
79 | static int marg_t, marg_b; /* scroll margins */ |
80 | static int dec_om; /* DEC origin mode flag */ |
81 | static int wrap, wrapnext; /* wrap flags */ |
82 | static int insert; /* insert-mode flag */ |
83 | static int cset; /* 0 or 1: which char set */ |
84 | static int save_cset, save_csattr; /* saved with cursor position */ |
85 | static int rvideo; /* global reverse video flag */ |
e14a5a13 |
86 | static int cursor_on; /* cursor enabled flag */ |
87 | static int reset_132; /* Flag ESC c resets to 80 cols */ |
88 | static int use_bce; /* Use Background coloured erase */ |
89 | static int blinker; /* When blinking is the cursor on ? */ |
c9def1b8 |
90 | static int tblinker; /* When the blinking text is on */ |
91 | static int blink_is_real; /* Actually blink blinking text */ |
0965bee0 |
92 | static int term_echoing; /* Does terminal want local echo? */ |
93 | static int term_editing; /* Does terminal want local edit? */ |
374330e2 |
94 | |
95 | static unsigned long cset_attr[2]; |
96 | |
97 | /* |
98 | * Saved settings on the alternate screen. |
99 | */ |
100 | static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset; |
101 | static int alt_t, alt_b; |
102 | static int alt_which; |
103 | |
104 | #define ARGS_MAX 32 /* max # of esc sequence arguments */ |
064916ea |
105 | #define ARG_DEFAULT 0 /* if an arg isn't specified */ |
374330e2 |
106 | #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) ) |
107 | static int esc_args[ARGS_MAX]; |
108 | static int esc_nargs; |
109 | static int esc_query; |
8b811b12 |
110 | #define ANSI(x,y) ((x)+((y)<<8)) |
111 | #define ANSI_QUE(x) ANSI(x,TRUE) |
374330e2 |
112 | |
113 | #define OSC_STR_MAX 2048 |
114 | static int osc_strlen; |
115 | static char osc_string[OSC_STR_MAX+1]; |
116 | static int osc_w; |
117 | |
c9def1b8 |
118 | static char id_string[1024] = "\033[?6c"; |
119 | |
374330e2 |
120 | static unsigned char *tabs; |
121 | |
374330e2 |
122 | static enum { |
cabfd08c |
123 | TOPLEVEL, |
124 | SEEN_ESC, |
125 | SEEN_CSI, |
126 | SEEN_OSC, |
127 | SEEN_OSC_W, |
128 | |
129 | DO_CTRLS, |
130 | |
131 | IGNORE_NEXT, |
132 | SET_GL, SET_GR, |
133 | SEEN_OSC_P, |
134 | OSC_STRING, OSC_MAYBE_ST, |
e14a5a13 |
135 | SEEN_ESCHASH, |
136 | VT52_ESC, |
137 | VT52_Y1, |
138 | VT52_Y2 |
374330e2 |
139 | } termstate; |
140 | |
141 | static enum { |
142 | NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED |
143 | } selstate; |
144 | static enum { |
145 | SM_CHAR, SM_WORD, SM_LINE |
146 | } selmode; |
147 | static unsigned long *selstart, *selend, *selanchor; |
148 | |
149 | static short wordness[256] = { |
150 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */ |
151 | 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */ |
152 | 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */ |
153 | 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */ |
154 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */ |
155 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */ |
156 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */ |
157 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */ |
158 | }; |
159 | |
160 | static unsigned char sel_nl[] = SEL_NL; |
c9def1b8 |
161 | static char * paste_buffer = 0; |
162 | static int paste_len, paste_pos, paste_hold; |
374330e2 |
163 | |
164 | /* |
165 | * Internal prototypes. |
166 | */ |
167 | static void do_paint (Context, int); |
168 | static void erase_lots (int, int, int); |
169 | static void swap_screen (int); |
170 | static void update_sbar (void); |
171 | static void deselect (void); |
e1c8e0ed |
172 | /* log session to file stuff ... */ |
173 | static FILE *lgfp = NULL; |
174 | static void logtraffic(unsigned char c, int logmode); |
374330e2 |
175 | |
176 | /* |
177 | * Set up power-on settings for the terminal. |
178 | */ |
179 | static void power_on(void) { |
180 | curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0; |
181 | alt_t = marg_t = 0; |
182 | if (rows != -1) |
183 | alt_b = marg_b = rows - 1; |
184 | else |
185 | alt_b = marg_b = 0; |
186 | if (cols != -1) { |
187 | int i; |
188 | for (i = 0; i < cols; i++) |
189 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
190 | } |
191 | alt_om = dec_om = cfg.dec_om; |
192 | alt_wnext = wrapnext = alt_ins = insert = FALSE; |
193 | alt_wrap = wrap = cfg.wrap_mode; |
194 | alt_cset = cset = 0; |
195 | cset_attr[0] = cset_attr[1] = ATTR_ASCII; |
196 | rvideo = 0; |
156686ef |
197 | in_vbell = FALSE; |
e14a5a13 |
198 | cursor_on = 1; |
374330e2 |
199 | save_attr = curr_attr = ATTR_DEFAULT; |
0965bee0 |
200 | term_editing = term_echoing = FALSE; |
201 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
374330e2 |
202 | app_cursor_keys = cfg.app_cursor; |
203 | app_keypad_keys = cfg.app_keypad; |
c9def1b8 |
204 | use_bce = cfg.bce; |
205 | blink_is_real = cfg.blinktext; |
e14a5a13 |
206 | erase_char = ERASE_CHAR; |
374330e2 |
207 | alt_which = 0; |
208 | { |
209 | int i; |
210 | for (i = 0; i < 256; i++) |
211 | wordness[i] = cfg.wordness[i]; |
212 | } |
213 | if (text) { |
214 | swap_screen (1); |
215 | erase_lots (FALSE, TRUE, TRUE); |
216 | swap_screen (0); |
217 | erase_lots (FALSE, TRUE, TRUE); |
218 | } |
219 | } |
220 | |
221 | /* |
222 | * Force a screen update. |
223 | */ |
224 | void term_update(void) { |
225 | Context ctx; |
226 | ctx = get_ctx(); |
227 | if (ctx) { |
c9def1b8 |
228 | if ( (seen_key_event && (cfg.scroll_on_key)) || |
a094ae43 |
229 | (seen_disp_event && (cfg.scroll_on_disp)) ) { |
cabfd08c |
230 | disptop = scrtop; |
231 | seen_disp_event = seen_key_event = 0; |
8ac3e544 |
232 | update_sbar(); |
cabfd08c |
233 | } |
374330e2 |
234 | do_paint (ctx, TRUE); |
ec8679e9 |
235 | sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1)); |
374330e2 |
236 | free_ctx (ctx); |
374330e2 |
237 | } |
238 | } |
239 | |
240 | /* |
241 | * Same as power_on(), but an external function. |
242 | */ |
243 | void term_pwron(void) { |
244 | power_on(); |
245 | fix_cpos; |
246 | disptop = scrtop; |
247 | deselect(); |
248 | term_update(); |
249 | } |
250 | |
251 | /* |
252 | * Clear the scrollback. |
253 | */ |
254 | void term_clrsb(void) { |
255 | disptop = sbtop = scrtop; |
256 | update_sbar(); |
257 | } |
258 | |
259 | /* |
260 | * Initialise the terminal. |
261 | */ |
262 | void term_init(void) { |
c9def1b8 |
263 | text = sbtop = sbbot = scrtop = disptop = cpos = NULL; |
374330e2 |
264 | disptext = wanttext = NULL; |
265 | tabs = NULL; |
266 | selspace = NULL; |
267 | deselect(); |
268 | rows = cols = -1; |
374330e2 |
269 | power_on(); |
156686ef |
270 | beephead = beeptail = NULL; |
271 | nbeeps = 0; |
272 | lastbeep = FALSE; |
273 | beep_overloaded = FALSE; |
374330e2 |
274 | } |
275 | |
276 | /* |
277 | * Set up the terminal for a given size. |
278 | */ |
279 | void term_size(int newrows, int newcols, int newsavelines) { |
280 | unsigned long *newtext, *newdisp, *newwant, *newalt; |
281 | int i, j, crows, ccols; |
282 | |
d6832394 |
283 | int save_alt_which = alt_which; |
284 | |
374330e2 |
285 | if (newrows == rows && newcols == cols && newsavelines == savelines) |
286 | return; /* nothing to do */ |
287 | |
d6832394 |
288 | deselect(); |
289 | swap_screen(0); |
290 | |
374330e2 |
291 | alt_t = marg_t = 0; |
292 | alt_b = marg_b = newrows - 1; |
293 | |
294 | newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE); |
c9def1b8 |
295 | sbbot = newtext + newsavelines*(newcols+1); |
374330e2 |
296 | for (i=0; i<(newrows+newsavelines)*(newcols+1); i++) |
c9def1b8 |
297 | newtext[i] = erase_char; |
374330e2 |
298 | if (rows != -1) { |
299 | crows = rows + (scrtop - sbtop) / (cols+1); |
300 | if (crows > newrows+newsavelines) |
301 | crows = newrows+newsavelines; |
c9def1b8 |
302 | if (newrows>crows) |
303 | disptop = newtext; |
304 | else |
305 | disptop = newtext + (crows-newrows)*(newcols+1); |
374330e2 |
306 | ccols = (cols < newcols ? cols : newcols); |
307 | for (i=0; i<crows; i++) { |
c9def1b8 |
308 | int oldidx = (rows - crows + i) * (cols+1); |
309 | int newidx = (newrows - crows + i) * (newcols+1); |
310 | |
374330e2 |
311 | for (j=0; j<ccols; j++) |
c9def1b8 |
312 | disptop[newidx+j] = scrtop[oldidx+j]; |
313 | disptop[newidx+newcols] = |
314 | (cols == newcols ? scrtop[oldidx+cols] |
315 | : (scrtop[oldidx+cols]&LATTR_MODE)); |
374330e2 |
316 | } |
c9def1b8 |
317 | sbtop = newtext; |
318 | } else { |
319 | sbtop = disptop = newtext; |
320 | } |
374330e2 |
321 | scrtop = disptop; |
322 | sfree (text); |
323 | text = newtext; |
324 | |
325 | newdisp = smalloc (newrows*(newcols+1)*TSIZE); |
326 | for (i=0; i<newrows*(newcols+1); i++) |
327 | newdisp[i] = ATTR_INVALID; |
328 | sfree (disptext); |
329 | disptext = newdisp; |
330 | |
331 | newwant = smalloc (newrows*(newcols+1)*TSIZE); |
332 | for (i=0; i<newrows*(newcols+1); i++) |
333 | newwant[i] = ATTR_INVALID; |
334 | sfree (wanttext); |
335 | wanttext = newwant; |
336 | |
337 | newalt = smalloc (newrows*(newcols+1)*TSIZE); |
338 | for (i=0; i<newrows*(newcols+1); i++) |
e14a5a13 |
339 | newalt[i] = erase_char; |
374330e2 |
340 | sfree (alttext); |
341 | alttext = newalt; |
342 | |
343 | sfree (selspace); |
344 | selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) ); |
345 | |
346 | tabs = srealloc (tabs, newcols*sizeof(*tabs)); |
347 | { |
348 | int i; |
349 | for (i = (cols > 0 ? cols : 0); i < newcols; i++) |
350 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
351 | } |
352 | |
353 | if (rows > 0) |
354 | curs_y += newrows - rows; |
355 | if (curs_y < 0) |
356 | curs_y = 0; |
357 | if (curs_y >= newrows) |
358 | curs_y = newrows-1; |
359 | if (curs_x >= newcols) |
360 | curs_x = newcols-1; |
361 | alt_x = alt_y = 0; |
362 | wrapnext = alt_wnext = FALSE; |
363 | |
364 | rows = newrows; |
365 | cols = newcols; |
366 | savelines = newsavelines; |
367 | fix_cpos; |
368 | |
d6832394 |
369 | swap_screen(save_alt_which); |
370 | |
374330e2 |
371 | update_sbar(); |
372 | term_update(); |
373 | } |
374 | |
375 | /* |
376 | * Swap screens. |
377 | */ |
378 | static void swap_screen (int which) { |
379 | int t; |
380 | unsigned long tt; |
381 | |
382 | if (which == alt_which) |
383 | return; |
384 | |
385 | alt_which = which; |
386 | |
387 | for (t=0; t<rows*(cols+1); t++) { |
388 | tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt; |
389 | } |
390 | |
391 | t = curs_x; curs_x = alt_x; alt_x = t; |
392 | t = curs_y; curs_y = alt_y; alt_y = t; |
393 | t = marg_t; marg_t = alt_t; alt_t = t; |
394 | t = marg_b; marg_b = alt_b; alt_b = t; |
395 | t = dec_om; dec_om = alt_om; alt_om = t; |
396 | t = wrap; wrap = alt_wrap; alt_wrap = t; |
397 | t = wrapnext; wrapnext = alt_wnext; alt_wnext = t; |
398 | t = insert; insert = alt_ins; alt_ins = t; |
399 | t = cset; cset = alt_cset; alt_cset = t; |
400 | |
401 | fix_cpos; |
402 | } |
403 | |
404 | /* |
374330e2 |
405 | * Update the scroll bar. |
406 | */ |
407 | static void update_sbar(void) { |
408 | int min; |
409 | |
410 | min = (sbtop - text) / (cols+1); |
411 | set_sbar ((scrtop - text) / (cols+1) + rows - min, |
412 | (disptop - text) / (cols+1) - min, |
413 | rows); |
414 | } |
415 | |
416 | /* |
417 | * Check whether the region bounded by the two pointers intersects |
418 | * the scroll region, and de-select the on-screen selection if so. |
419 | */ |
420 | static void check_selection (unsigned long *from, unsigned long *to) { |
421 | if (from < selend && selstart < to) |
422 | deselect(); |
423 | } |
424 | |
425 | /* |
426 | * Scroll the screen. (`lines' is +ve for scrolling forward, -ve |
427 | * for backward.) `sb' is TRUE if the scrolling is permitted to |
428 | * affect the scrollback buffer. |
429 | */ |
430 | static void scroll (int topline, int botline, int lines, int sb) { |
431 | unsigned long *scroll_top; |
c9def1b8 |
432 | unsigned long *newscr; |
374330e2 |
433 | int scroll_size, size, i; |
c9def1b8 |
434 | int scrtop_is_disptop = (scrtop==disptop); |
435 | static int recursive = 0; |
436 | |
437 | /* Only scroll more than the window if we're doing a 10% scroll */ |
438 | if (!recursive && lines > botline - topline + 1) |
439 | lines = botline - topline + 1; |
374330e2 |
440 | |
441 | scroll_top = scrtop + topline*(cols+1); |
442 | size = (lines < 0 ? -lines : lines) * (cols+1); |
443 | scroll_size = (botline - topline + 1) * (cols+1) - size; |
444 | |
cabfd08c |
445 | if (lines > 0 && topline == 0 && alt_which == 0 && sb) { |
374330e2 |
446 | /* |
cabfd08c |
447 | * Since we're going to scroll the top line and we're on the |
c9def1b8 |
448 | * scrolling screen let's also effect the scrollback buffer. |
449 | * |
450 | * This is normally done by moving the position the screen |
451 | * painter reads from to reduce the amount of memory copying |
452 | * required. |
374330e2 |
453 | */ |
c9def1b8 |
454 | if (scroll_size >= 0 && !recursive) { |
455 | newscr = scrtop + lines * (cols+1); |
456 | |
457 | if (newscr > sbbot && botline == rows-1) { |
458 | /* We've hit the bottom of memory, so we have to do a |
459 | * physical scroll. But instead of just 1 line do it |
460 | * by 10% of the available memory. |
461 | * |
462 | * If the scroll region isn't the whole screen then we can't |
463 | * do this as it stands. We would need to recover the bottom |
464 | * of the screen from the scroll buffer after being sure that |
465 | * it doesn't get deleted. |
466 | */ |
467 | |
468 | i = (rows+savelines)/10; |
469 | |
470 | /* Make it simple and ensure safe recursion */ |
471 | if ( i<savelines-1) { |
472 | recursive ++; |
473 | scroll(topline, botline, i, sb); |
474 | recursive --; |
475 | |
476 | newscr = scrtop - i * (cols+1); |
477 | if (scrtop_is_disptop) disptop = newscr; |
478 | scrtop = newscr; |
479 | } |
480 | |
481 | newscr = scrtop + lines * (cols+1); |
482 | } |
483 | |
484 | if (newscr <= sbbot) { |
485 | if (scrtop_is_disptop) disptop = newscr; |
486 | scrtop = newscr; |
487 | |
488 | if (botline == rows-1 ) |
489 | for (i = 0; i < size; i++) |
490 | scrtop[i+scroll_size] = erase_char; |
491 | |
492 | update_sbar(); |
493 | fix_cpos; |
494 | |
495 | if (botline != rows-1) { |
496 | /* This fastscroll only works for full window scrolls. |
497 | * If the caller wanted a partial one we have to reverse |
498 | * scroll the bottom of the screen. |
499 | */ |
500 | scroll(botline-lines+1, rows-1, -lines, 0); |
501 | } |
502 | return ; |
503 | } |
504 | |
505 | /* If we can't scroll by memory remapping do it physically. |
506 | * But rather than expensivly doing the scroll buffer just |
507 | * scroll the screen. All it means is that sometimes we choose |
508 | * to not add lines from a scroll region to the scroll buffer. |
509 | */ |
510 | |
511 | if (savelines <= 400) { |
512 | sbtop -= lines * (cols+1); |
513 | if (sbtop < text) |
514 | sbtop = text; |
515 | scroll_size += scroll_top - sbtop; |
516 | scroll_top = sbtop; |
517 | |
518 | update_sbar(); |
519 | } |
520 | } else { |
521 | /* Ho hum, expensive scroll required. */ |
522 | |
523 | sbtop -= lines * (cols+1); |
524 | if (sbtop < text) |
525 | sbtop = text; |
526 | scroll_size += scroll_top - sbtop; |
527 | scroll_top = sbtop; |
528 | |
529 | update_sbar(); |
530 | } |
374330e2 |
531 | } |
532 | |
533 | if (scroll_size < 0) { |
534 | size += scroll_size; |
535 | scroll_size = 0; |
536 | } |
537 | |
538 | if (lines > 0) { |
539 | if (scroll_size) |
540 | memmove (scroll_top, scroll_top + size, scroll_size*TSIZE); |
541 | for (i = 0; i < size; i++) |
e14a5a13 |
542 | scroll_top[i+scroll_size] = erase_char; |
374330e2 |
543 | if (selstart > scroll_top && |
544 | selstart < scroll_top + size + scroll_size) { |
545 | selstart -= size; |
546 | if (selstart < scroll_top) |
547 | selstart = scroll_top; |
548 | } |
549 | if (selend > scroll_top && |
550 | selend < scroll_top + size + scroll_size) { |
551 | selend -= size; |
552 | if (selend < scroll_top) |
553 | selend = scroll_top; |
554 | } |
c9def1b8 |
555 | if (scrtop_is_disptop) |
556 | disptop = scrtop; |
557 | else |
558 | if (disptop > scroll_top && |
559 | disptop < scroll_top + size + scroll_size) { |
560 | disptop -= size; |
561 | if (disptop < scroll_top) |
562 | disptop = scroll_top; |
563 | } |
374330e2 |
564 | } else { |
565 | if (scroll_size) |
566 | memmove (scroll_top + size, scroll_top, scroll_size*TSIZE); |
567 | for (i = 0; i < size; i++) |
e14a5a13 |
568 | scroll_top[i] = erase_char; |
374330e2 |
569 | if (selstart > scroll_top && |
570 | selstart < scroll_top + size + scroll_size) { |
571 | selstart += size; |
572 | if (selstart > scroll_top + size + scroll_size) |
573 | selstart = scroll_top + size + scroll_size; |
574 | } |
575 | if (selend > scroll_top && |
576 | selend < scroll_top + size + scroll_size) { |
577 | selend += size; |
578 | if (selend > scroll_top + size + scroll_size) |
579 | selend = scroll_top + size + scroll_size; |
580 | } |
c9def1b8 |
581 | if (scrtop_is_disptop) |
582 | disptop = scrtop; |
583 | else if (disptop > scroll_top && |
584 | disptop < scroll_top + size + scroll_size) { |
585 | disptop += size; |
586 | if (disptop > scroll_top + size + scroll_size) |
587 | disptop = scroll_top + size + scroll_size; |
588 | } |
374330e2 |
589 | } |
374330e2 |
590 | } |
591 | |
592 | /* |
593 | * Move the cursor to a given position, clipping at boundaries. We |
594 | * may or may not want to clip at the scroll margin: marg_clip is 0 |
595 | * not to, 1 to disallow _passing_ the margins, and 2 to disallow |
596 | * even _being_ outside the margins. |
597 | */ |
598 | static void move (int x, int y, int marg_clip) { |
599 | if (x < 0) |
600 | x = 0; |
601 | if (x >= cols) |
602 | x = cols-1; |
603 | if (marg_clip) { |
604 | if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t) |
605 | y = marg_t; |
606 | if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b) |
607 | y = marg_b; |
608 | } |
609 | if (y < 0) |
610 | y = 0; |
611 | if (y >= rows) |
612 | y = rows-1; |
613 | curs_x = x; |
614 | curs_y = y; |
615 | fix_cpos; |
616 | wrapnext = FALSE; |
617 | } |
618 | |
619 | /* |
620 | * Save or restore the cursor and SGR mode. |
621 | */ |
622 | static void save_cursor(int save) { |
623 | if (save) { |
624 | save_x = curs_x; |
625 | save_y = curs_y; |
626 | save_attr = curr_attr; |
627 | save_cset = cset; |
628 | save_csattr = cset_attr[cset]; |
629 | } else { |
630 | curs_x = save_x; |
631 | curs_y = save_y; |
c9def1b8 |
632 | /* Make sure the window hasn't shrunk since the save */ |
633 | if (curs_x >= cols) curs_x = cols-1; |
634 | if (curs_y >= rows) curs_y = rows-1; |
635 | |
374330e2 |
636 | curr_attr = save_attr; |
637 | cset = save_cset; |
638 | cset_attr[cset] = save_csattr; |
639 | fix_cpos; |
e14a5a13 |
640 | if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
374330e2 |
641 | } |
642 | } |
643 | |
644 | /* |
645 | * Erase a large portion of the screen: the whole screen, or the |
646 | * whole line, or parts thereof. |
647 | */ |
648 | static void erase_lots (int line_only, int from_begin, int to_end) { |
649 | unsigned long *startpos, *endpos; |
650 | |
651 | if (line_only) { |
652 | startpos = cpos - curs_x; |
c9def1b8 |
653 | endpos = startpos + cols; |
654 | /* I've removed the +1 so that the Wide screen stuff is not |
655 | * removed when it shouldn't be. |
656 | */ |
374330e2 |
657 | } else { |
658 | startpos = scrtop; |
659 | endpos = startpos + rows * (cols+1); |
660 | } |
661 | if (!from_begin) |
662 | startpos = cpos; |
663 | if (!to_end) |
e14a5a13 |
664 | endpos = cpos+1; |
374330e2 |
665 | check_selection (startpos, endpos); |
e14a5a13 |
666 | |
667 | /* Clear screen also forces a full window redraw, just in case. */ |
668 | if (startpos == scrtop && endpos == scrtop + rows * (cols+1)) |
669 | term_invalidate(); |
670 | |
374330e2 |
671 | while (startpos < endpos) |
e14a5a13 |
672 | *startpos++ = erase_char; |
374330e2 |
673 | } |
674 | |
675 | /* |
676 | * Insert or delete characters within the current line. n is +ve if |
677 | * insertion is desired, and -ve for deletion. |
678 | */ |
679 | static void insch (int n) { |
680 | int dir = (n < 0 ? -1 : +1); |
681 | int m; |
682 | |
683 | n = (n < 0 ? -n : n); |
684 | if (n > cols - curs_x) |
685 | n = cols - curs_x; |
686 | m = cols - curs_x - n; |
687 | check_selection (cpos, cpos+n); |
688 | if (dir < 0) { |
689 | memmove (cpos, cpos+n, m*TSIZE); |
690 | while (n--) |
e14a5a13 |
691 | cpos[m++] = erase_char; |
374330e2 |
692 | } else { |
693 | memmove (cpos+n, cpos, m*TSIZE); |
694 | while (n--) |
e14a5a13 |
695 | cpos[n] = erase_char; |
374330e2 |
696 | } |
697 | } |
698 | |
699 | /* |
700 | * Toggle terminal mode `mode' to state `state'. (`query' indicates |
701 | * whether the mode is a DEC private one or a normal one.) |
702 | */ |
703 | static void toggle_mode (int mode, int query, int state) { |
704 | if (query) switch (mode) { |
705 | case 1: /* application cursor keys */ |
706 | app_cursor_keys = state; |
707 | break; |
e14a5a13 |
708 | case 2: /* VT52 mode */ |
709 | vt52_mode = !state; |
710 | break; |
374330e2 |
711 | case 3: /* 80/132 columns */ |
712 | deselect(); |
e14a5a13 |
713 | request_resize (state ? 132 : 80, rows, 1); |
714 | reset_132 = state; |
374330e2 |
715 | break; |
716 | case 5: /* reverse video */ |
717 | rvideo = state; |
cabfd08c |
718 | seen_disp_event = TRUE; |
e14a5a13 |
719 | if (state) term_update(); |
374330e2 |
720 | break; |
721 | case 6: /* DEC origin mode */ |
722 | dec_om = state; |
723 | break; |
724 | case 7: /* auto wrap */ |
725 | wrap = state; |
726 | break; |
c9def1b8 |
727 | case 8: /* auto key repeat */ |
728 | repeat_off = !state; |
729 | break; |
0965bee0 |
730 | case 10: /* set local edit mode */ |
731 | term_editing = state; |
732 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
733 | break; |
e14a5a13 |
734 | case 25: /* enable/disable cursor */ |
ec55b220 |
735 | compatibility2(OTHER,VT220); |
e14a5a13 |
736 | cursor_on = state; |
737 | seen_disp_event = TRUE; |
738 | break; |
374330e2 |
739 | case 47: /* alternate screen */ |
e14a5a13 |
740 | compatibility(OTHER); |
374330e2 |
741 | deselect(); |
742 | swap_screen (state); |
743 | disptop = scrtop; |
744 | break; |
745 | } else switch (mode) { |
746 | case 4: /* set insert mode */ |
e14a5a13 |
747 | compatibility(VT102); |
374330e2 |
748 | insert = state; |
749 | break; |
e14a5a13 |
750 | case 12: /* set echo mode */ |
0965bee0 |
751 | term_echoing = !state; |
752 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
e14a5a13 |
753 | break; |
c9def1b8 |
754 | case 20: /* Return sends ... */ |
755 | cr_lf_return = state; |
756 | break; |
374330e2 |
757 | } |
758 | } |
759 | |
760 | /* |
761 | * Process an OSC sequence: set window title or icon name. |
762 | */ |
763 | static void do_osc(void) { |
764 | if (osc_w) { |
765 | while (osc_strlen--) |
766 | wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0]; |
767 | } else { |
768 | osc_string[osc_strlen] = '\0'; |
769 | switch (esc_args[0]) { |
770 | case 0: |
771 | case 1: |
772 | set_icon (osc_string); |
773 | if (esc_args[0] == 1) |
774 | break; |
775 | /* fall through: parameter 0 means set both */ |
776 | case 2: |
777 | case 21: |
778 | set_title (osc_string); |
779 | break; |
780 | } |
781 | } |
782 | } |
783 | |
784 | /* |
785 | * Remove everything currently in `inbuf' and stick it up on the |
786 | * in-memory display. There's a big state machine in here to |
787 | * process escape sequences... |
788 | */ |
789 | void term_out(void) { |
c9def1b8 |
790 | int c, inbuf_reap; |
791 | |
c9def1b8 |
792 | for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++) |
793 | { |
794 | c = inbuf[inbuf_reap]; |
374330e2 |
795 | |
c9def1b8 |
796 | /* |
5fd04f07 |
797 | * Optionally log the session traffic to a file. Useful for |
798 | * debugging and possibly also useful for actual logging. |
799 | */ |
e1c8e0ed |
800 | logtraffic((unsigned char)c, LGTYP_DEBUG); |
801 | |
e14a5a13 |
802 | /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even |
803 | * be able to display 8-bit characters, but I'll let that go 'cause |
804 | * of i18n. |
805 | */ |
c9def1b8 |
806 | if( ( (c&0x60) == 0 || c == '\177') && |
807 | termstate < DO_CTRLS && |
e14a5a13 |
808 | ( (c&0x80) == 0 || has_compat(VT220))) { |
374330e2 |
809 | switch (c) { |
810 | case '\005': /* terminal type query */ |
e14a5a13 |
811 | /* Strictly speaking this is VT100 but a VT100 defaults to |
812 | * no response. Other terminals respond at their option. |
813 | * |
814 | * Don't put a CR in the default string as this tends to |
815 | * upset some weird software. |
816 | * |
817 | * An xterm returns "xterm" (5 characters) |
818 | */ |
e7fbcdd8 |
819 | compatibility(ANSIMIN); |
820 | { |
821 | char abuf[256], *s, *d; |
822 | int state=0; |
823 | for(s=cfg.answerback, d=abuf; *s; s++) { |
824 | if (state) |
825 | { |
826 | if (*s >= 'a' && *s <= 'z') |
827 | *d++ = (*s - ('a'-1)); |
828 | else if ((*s >='@' && *s<='_') || |
829 | *s == '?' || (*s&0x80)) |
830 | *d++ = ('@'^*s); |
831 | else if (*s == '~') |
832 | *d++ = '^'; |
833 | state = 0; |
834 | } |
835 | else if (*s == '^') { |
836 | state = 1; |
837 | } |
838 | else |
839 | *d++ = xlat_kbd2tty((unsigned char)*s); |
840 | } |
841 | ldisc_send (abuf, d-abuf); |
842 | } |
374330e2 |
843 | break; |
844 | case '\007': |
156686ef |
845 | { |
846 | struct beeptime *newbeep; |
847 | long ticks; |
848 | |
849 | ticks = GetTickCount(); |
156686ef |
850 | |
851 | if (!beep_overloaded) { |
852 | newbeep = smalloc(sizeof(struct beeptime)); |
853 | newbeep->ticks = ticks; |
854 | newbeep->next = NULL; |
855 | if (!beephead) |
856 | beephead = newbeep; |
857 | else |
858 | beeptail->next = newbeep; |
859 | beeptail = newbeep; |
860 | nbeeps++; |
861 | } |
862 | |
863 | /* |
864 | * Throw out any beeps that happened more than |
865 | * t seconds ago. |
866 | */ |
867 | while (beephead && |
868 | beephead->ticks < ticks - cfg.bellovl_t*1000) { |
869 | struct beeptime *tmp = beephead; |
870 | beephead = tmp->next; |
156686ef |
871 | sfree(tmp); |
872 | if (!beephead) |
873 | beeptail = NULL; |
874 | nbeeps--; |
875 | } |
876 | |
877 | if (cfg.bellovl && beep_overloaded && |
878 | ticks-lastbeep >= cfg.bellovl_s * 1000) { |
879 | /* |
880 | * If we're currently overloaded and the |
881 | * last beep was more than s seconds ago, |
882 | * leave overload mode. |
883 | */ |
156686ef |
884 | beep_overloaded = FALSE; |
885 | } else if (cfg.bellovl && !beep_overloaded && |
886 | nbeeps >= cfg.bellovl_n) { |
887 | /* |
888 | * Now, if we have n or more beeps |
889 | * remaining in the queue, go into overload |
890 | * mode. |
891 | */ |
156686ef |
892 | beep_overloaded = TRUE; |
893 | } |
894 | lastbeep = ticks; |
895 | |
896 | /* |
897 | * Perform an actual beep if we're not overloaded. |
898 | */ |
899 | if ((!cfg.bellovl || !beep_overloaded) && cfg.beep != 0) { |
156686ef |
900 | if (cfg.beep != 2) |
901 | beep(cfg.beep); |
902 | else if(cfg.beep == 2) { |
903 | in_vbell = TRUE; |
904 | vbell_timeout = ticks + VBELL_TIMEOUT; |
905 | term_update(); |
906 | } |
907 | } |
908 | disptop = scrtop; |
909 | } |
374330e2 |
910 | break; |
911 | case '\b': |
c9def1b8 |
912 | if (curs_x == 0 && curs_y == 0) |
913 | ; |
914 | else if (curs_x == 0 && curs_y > 0) |
374330e2 |
915 | curs_x = cols-1, curs_y--; |
916 | else if (wrapnext) |
917 | wrapnext = FALSE; |
918 | else |
919 | curs_x--; |
920 | fix_cpos; |
cabfd08c |
921 | seen_disp_event = TRUE; |
374330e2 |
922 | break; |
923 | case '\016': |
e14a5a13 |
924 | compatibility(VT100); |
374330e2 |
925 | cset = 1; |
926 | break; |
927 | case '\017': |
e14a5a13 |
928 | compatibility(VT100); |
374330e2 |
929 | cset = 0; |
930 | break; |
931 | case '\033': |
e14a5a13 |
932 | if (vt52_mode) |
933 | termstate = VT52_ESC; |
934 | else { |
935 | compatibility(ANSIMIN); |
936 | termstate = SEEN_ESC; |
937 | } |
374330e2 |
938 | break; |
939 | case 0233: |
e14a5a13 |
940 | compatibility(VT220); |
374330e2 |
941 | termstate = SEEN_CSI; |
942 | esc_nargs = 1; |
943 | esc_args[0] = ARG_DEFAULT; |
944 | esc_query = FALSE; |
945 | break; |
946 | case 0235: |
e14a5a13 |
947 | compatibility(VT220); |
374330e2 |
948 | termstate = SEEN_OSC; |
949 | esc_args[0] = 0; |
950 | break; |
951 | case '\r': |
952 | curs_x = 0; |
953 | wrapnext = FALSE; |
954 | fix_cpos; |
cabfd08c |
955 | seen_disp_event = TRUE; |
c9def1b8 |
956 | paste_hold = 0; |
e1c8e0ed |
957 | logtraffic((unsigned char)c,LGTYP_ASCII); |
374330e2 |
958 | break; |
959 | case '\013': |
960 | case '\014': |
e14a5a13 |
961 | compatibility(VT100); |
374330e2 |
962 | case '\n': |
963 | if (curs_y == marg_b) |
964 | scroll (marg_t, marg_b, 1, TRUE); |
965 | else if (curs_y < rows-1) |
966 | curs_y++; |
cabfd08c |
967 | if (cfg.lfhascr) |
968 | curs_x = 0; |
374330e2 |
969 | fix_cpos; |
970 | wrapnext = FALSE; |
cabfd08c |
971 | seen_disp_event = 1; |
c9def1b8 |
972 | paste_hold = 0; |
e1c8e0ed |
973 | logtraffic((unsigned char)c,LGTYP_ASCII); |
374330e2 |
974 | break; |
975 | case '\t': |
374330e2 |
976 | { |
977 | unsigned long *old_cpos = cpos; |
c9def1b8 |
978 | unsigned long *p = scrtop + curs_y * (cols+1) + cols; |
979 | |
980 | do { |
981 | curs_x++; |
982 | } while (curs_x < cols-1 && !tabs[curs_x]); |
983 | |
984 | if ((*p & LATTR_MODE) != LATTR_NORM) |
985 | { |
986 | if (curs_x >= cols/2) |
987 | curs_x = cols/2-1; |
988 | } |
989 | else |
990 | { |
991 | if (curs_x >= cols) |
992 | curs_x = cols-1; |
993 | } |
994 | |
374330e2 |
995 | fix_cpos; |
996 | check_selection (old_cpos, cpos); |
997 | } |
cabfd08c |
998 | seen_disp_event = TRUE; |
374330e2 |
999 | break; |
1ec0fa4b |
1000 | case '\177': /* Destructive backspace |
ec55b220 |
1001 | This does nothing on a real VT100 */ |
1002 | compatibility(OTHER); |
1003 | if (curs_x && !wrapnext) curs_x--; |
1004 | wrapnext = FALSE; |
1005 | fix_cpos; |
1006 | *cpos = (' ' | curr_attr | ATTR_ASCII); |
1007 | break; |
cabfd08c |
1008 | } |
1009 | } |
1010 | else switch (termstate) { |
1011 | case TOPLEVEL: |
8b811b12 |
1012 | /* Only graphic characters get this far, ctrls are stripped above */ |
1ec0fa4b |
1013 | if (wrapnext && wrap) { |
c9def1b8 |
1014 | cpos[1] |= ATTR_WRAPPED; |
8b811b12 |
1015 | if (curs_y == marg_b) |
1016 | scroll (marg_t, marg_b, 1, TRUE); |
1017 | else if (curs_y < rows-1) |
1018 | curs_y++; |
1019 | curs_x = 0; |
1020 | fix_cpos; |
1021 | wrapnext = FALSE; |
8b811b12 |
1022 | } |
1023 | if (insert) |
1024 | insch (1); |
e14a5a13 |
1025 | if (selstate != NO_SELECTION) |
1026 | check_selection (cpos, cpos+1); |
1027 | switch (cset_attr[cset]) { |
1028 | /* Linedraw characters are different from 'ESC ( B' only |
1029 | * for a small range, for ones outside that range make sure |
1030 | * we use the same font as well as the same encoding. |
1031 | */ |
1032 | case ATTR_LINEDRW: |
ec55b220 |
1033 | if (c<0x5f || c>0x7F) |
e14a5a13 |
1034 | *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr | |
1035 | ATTR_ASCII; |
ec55b220 |
1036 | else if (c==0x5F) |
1037 | *cpos++ = ' ' | curr_attr | ATTR_ASCII; |
e14a5a13 |
1038 | else |
1039 | *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW; |
1040 | break; |
ec55b220 |
1041 | case ATTR_GBCHR: |
1042 | /* If UK-ASCII, make the '#' a LineDraw Pound */ |
1043 | if (c == '#') { |
1044 | *cpos++ = '}' | curr_attr | ATTR_LINEDRW; |
1045 | break; |
1046 | } |
1047 | /*FALLTHROUGH*/ |
e14a5a13 |
1048 | default: |
e1c8e0ed |
1049 | *cpos = xlat_tty2scr((unsigned char)c) | curr_attr | |
e14a5a13 |
1050 | (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII); |
e1c8e0ed |
1051 | logtraffic((unsigned char)c, LGTYP_ASCII); |
1052 | cpos++; |
e14a5a13 |
1053 | break; |
1054 | } |
8b811b12 |
1055 | curs_x++; |
1056 | if (curs_x == cols) { |
1057 | cpos--; |
1058 | curs_x--; |
1ec0fa4b |
1059 | wrapnext = TRUE; |
374330e2 |
1060 | } |
8b811b12 |
1061 | seen_disp_event = 1; |
374330e2 |
1062 | break; |
cabfd08c |
1063 | |
374330e2 |
1064 | case IGNORE_NEXT: |
1065 | termstate = TOPLEVEL; |
1066 | break; |
1067 | case OSC_MAYBE_ST: |
1068 | /* |
1069 | * This state is virtually identical to SEEN_ESC, with the |
1070 | * exception that we have an OSC sequence in the pipeline, |
1071 | * and _if_ we see a backslash, we process it. |
1072 | */ |
1073 | if (c == '\\') { |
1074 | do_osc(); |
1075 | termstate = TOPLEVEL; |
1076 | break; |
1077 | } |
1078 | /* else fall through */ |
1079 | case SEEN_ESC: |
1080 | termstate = TOPLEVEL; |
1081 | switch (c) { |
374330e2 |
1082 | case ' ': /* some weird sequence? */ |
e14a5a13 |
1083 | compatibility(VT220); |
374330e2 |
1084 | termstate = IGNORE_NEXT; |
1085 | break; |
1086 | case '[': /* enter CSI mode */ |
1087 | termstate = SEEN_CSI; |
1088 | esc_nargs = 1; |
1089 | esc_args[0] = ARG_DEFAULT; |
1090 | esc_query = FALSE; |
1091 | break; |
1092 | case ']': /* xterm escape sequences */ |
e14a5a13 |
1093 | /* Compatibility is nasty here, xterm, linux, decterm yuk! */ |
1094 | compatibility(OTHER); |
374330e2 |
1095 | termstate = SEEN_OSC; |
1096 | esc_args[0] = 0; |
1097 | break; |
1098 | case '(': /* should set GL */ |
e14a5a13 |
1099 | compatibility(VT100); |
374330e2 |
1100 | termstate = SET_GL; |
1101 | break; |
1102 | case ')': /* should set GR */ |
e14a5a13 |
1103 | compatibility(VT100); |
374330e2 |
1104 | termstate = SET_GR; |
1105 | break; |
1106 | case '7': /* save cursor */ |
e14a5a13 |
1107 | compatibility(VT100); |
374330e2 |
1108 | save_cursor (TRUE); |
1109 | break; |
1110 | case '8': /* restore cursor */ |
e14a5a13 |
1111 | compatibility(VT100); |
374330e2 |
1112 | save_cursor (FALSE); |
cabfd08c |
1113 | seen_disp_event = TRUE; |
374330e2 |
1114 | break; |
1115 | case '=': |
e14a5a13 |
1116 | compatibility(VT100); |
374330e2 |
1117 | app_keypad_keys = TRUE; |
1118 | break; |
1119 | case '>': |
e14a5a13 |
1120 | compatibility(VT100); |
374330e2 |
1121 | app_keypad_keys = FALSE; |
1122 | break; |
1123 | case 'D': /* exactly equivalent to LF */ |
e14a5a13 |
1124 | compatibility(VT100); |
374330e2 |
1125 | if (curs_y == marg_b) |
1126 | scroll (marg_t, marg_b, 1, TRUE); |
1127 | else if (curs_y < rows-1) |
1128 | curs_y++; |
1129 | fix_cpos; |
1130 | wrapnext = FALSE; |
cabfd08c |
1131 | seen_disp_event = TRUE; |
374330e2 |
1132 | break; |
1133 | case 'E': /* exactly equivalent to CR-LF */ |
e14a5a13 |
1134 | compatibility(VT100); |
374330e2 |
1135 | curs_x = 0; |
374330e2 |
1136 | if (curs_y == marg_b) |
1137 | scroll (marg_t, marg_b, 1, TRUE); |
1138 | else if (curs_y < rows-1) |
1139 | curs_y++; |
1140 | fix_cpos; |
1141 | wrapnext = FALSE; |
cabfd08c |
1142 | seen_disp_event = TRUE; |
374330e2 |
1143 | break; |
1144 | case 'M': /* reverse index - backwards LF */ |
e14a5a13 |
1145 | compatibility(VT100); |
374330e2 |
1146 | if (curs_y == marg_t) |
1147 | scroll (marg_t, marg_b, -1, TRUE); |
1148 | else if (curs_y > 0) |
1149 | curs_y--; |
1150 | fix_cpos; |
1151 | wrapnext = FALSE; |
cabfd08c |
1152 | seen_disp_event = TRUE; |
374330e2 |
1153 | break; |
1154 | case 'Z': /* terminal type query */ |
e14a5a13 |
1155 | compatibility(VT100); |
0965bee0 |
1156 | ldisc_send (id_string, strlen(id_string)); |
374330e2 |
1157 | break; |
1158 | case 'c': /* restore power-on settings */ |
e14a5a13 |
1159 | compatibility(VT100); |
374330e2 |
1160 | power_on(); |
e14a5a13 |
1161 | if (reset_132) { |
1162 | request_resize (80, rows, 1); |
1163 | reset_132 = 0; |
1164 | } |
374330e2 |
1165 | fix_cpos; |
1166 | disptop = scrtop; |
cabfd08c |
1167 | seen_disp_event = TRUE; |
374330e2 |
1168 | break; |
1169 | case '#': /* ESC # 8 fills screen with Es :-) */ |
e14a5a13 |
1170 | compatibility(VT100); |
374330e2 |
1171 | termstate = SEEN_ESCHASH; |
1172 | break; |
1173 | case 'H': /* set a tab */ |
e14a5a13 |
1174 | compatibility(VT100); |
374330e2 |
1175 | tabs[curs_x] = TRUE; |
1176 | break; |
1177 | } |
1178 | break; |
1179 | case SEEN_CSI: |
1180 | termstate = TOPLEVEL; /* default */ |
8b811b12 |
1181 | if( isdigit(c) ) |
1182 | { |
374330e2 |
1183 | if (esc_nargs <= ARGS_MAX) { |
1184 | if (esc_args[esc_nargs-1] == ARG_DEFAULT) |
1185 | esc_args[esc_nargs-1] = 0; |
1186 | esc_args[esc_nargs-1] = |
1187 | 10 * esc_args[esc_nargs-1] + c - '0'; |
1188 | } |
1189 | termstate = SEEN_CSI; |
8b811b12 |
1190 | } |
1191 | else if( c == ';' ) |
1192 | { |
374330e2 |
1193 | if (++esc_nargs <= ARGS_MAX) |
1194 | esc_args[esc_nargs-1] = ARG_DEFAULT; |
1195 | termstate = SEEN_CSI; |
8b811b12 |
1196 | } |
1197 | else if( c < '@' ) |
1198 | { |
1199 | if( esc_query ) esc_query = -1; |
1200 | else if( c == '?' ) esc_query = TRUE; |
1201 | else esc_query = c; |
374330e2 |
1202 | termstate = SEEN_CSI; |
8b811b12 |
1203 | } |
1204 | else switch (ANSI(c,esc_query)) { |
374330e2 |
1205 | case 'A': /* move up N lines */ |
1206 | move (curs_x, curs_y - def(esc_args[0], 1), 1); |
cabfd08c |
1207 | seen_disp_event = TRUE; |
374330e2 |
1208 | break; |
e14a5a13 |
1209 | case 'e': /* move down N lines */ |
1210 | compatibility(ANSI); |
1211 | case 'B': |
374330e2 |
1212 | move (curs_x, curs_y + def(esc_args[0], 1), 1); |
cabfd08c |
1213 | seen_disp_event = TRUE; |
374330e2 |
1214 | break; |
e14a5a13 |
1215 | case 'a': /* move right N cols */ |
1216 | compatibility(ANSI); |
1217 | case 'C': |
374330e2 |
1218 | move (curs_x + def(esc_args[0], 1), curs_y, 1); |
cabfd08c |
1219 | seen_disp_event = TRUE; |
374330e2 |
1220 | break; |
1221 | case 'D': /* move left N cols */ |
1222 | move (curs_x - def(esc_args[0], 1), curs_y, 1); |
cabfd08c |
1223 | seen_disp_event = TRUE; |
374330e2 |
1224 | break; |
1225 | case 'E': /* move down N lines and CR */ |
e14a5a13 |
1226 | compatibility(ANSI); |
374330e2 |
1227 | move (0, curs_y + def(esc_args[0], 1), 1); |
cabfd08c |
1228 | seen_disp_event = TRUE; |
374330e2 |
1229 | break; |
1230 | case 'F': /* move up N lines and CR */ |
e14a5a13 |
1231 | compatibility(ANSI); |
374330e2 |
1232 | move (0, curs_y - def(esc_args[0], 1), 1); |
cabfd08c |
1233 | seen_disp_event = TRUE; |
374330e2 |
1234 | break; |
1235 | case 'G': case '`': /* set horizontal posn */ |
e14a5a13 |
1236 | compatibility(ANSI); |
374330e2 |
1237 | move (def(esc_args[0], 1) - 1, curs_y, 0); |
cabfd08c |
1238 | seen_disp_event = TRUE; |
374330e2 |
1239 | break; |
1240 | case 'd': /* set vertical posn */ |
e14a5a13 |
1241 | compatibility(ANSI); |
374330e2 |
1242 | move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
1243 | (dec_om ? 2 : 0)); |
cabfd08c |
1244 | seen_disp_event = TRUE; |
374330e2 |
1245 | break; |
1246 | case 'H': case 'f': /* set horz and vert posns at once */ |
1247 | if (esc_nargs < 2) |
1248 | esc_args[1] = ARG_DEFAULT; |
1249 | move (def(esc_args[1], 1) - 1, |
1250 | (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
1251 | (dec_om ? 2 : 0)); |
cabfd08c |
1252 | seen_disp_event = TRUE; |
374330e2 |
1253 | break; |
1254 | case 'J': /* erase screen or parts of it */ |
1255 | { |
1256 | unsigned int i = def(esc_args[0], 0) + 1; |
1257 | if (i > 3) |
1258 | i = 0; |
1259 | erase_lots(FALSE, !!(i & 2), !!(i & 1)); |
1260 | } |
1261 | disptop = scrtop; |
cabfd08c |
1262 | seen_disp_event = TRUE; |
374330e2 |
1263 | break; |
1264 | case 'K': /* erase line or parts of it */ |
1265 | { |
1266 | unsigned int i = def(esc_args[0], 0) + 1; |
1267 | if (i > 3) |
1268 | i = 0; |
1269 | erase_lots(TRUE, !!(i & 2), !!(i & 1)); |
1270 | } |
cabfd08c |
1271 | seen_disp_event = TRUE; |
374330e2 |
1272 | break; |
1273 | case 'L': /* insert lines */ |
e14a5a13 |
1274 | compatibility(VT102); |
374330e2 |
1275 | if (curs_y <= marg_b) |
1276 | scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE); |
cabfd08c |
1277 | seen_disp_event = TRUE; |
374330e2 |
1278 | break; |
1279 | case 'M': /* delete lines */ |
e14a5a13 |
1280 | compatibility(VT102); |
374330e2 |
1281 | if (curs_y <= marg_b) |
cabfd08c |
1282 | scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE); |
1283 | seen_disp_event = TRUE; |
374330e2 |
1284 | break; |
1285 | case '@': /* insert chars */ |
e14a5a13 |
1286 | /* XXX VTTEST says this is vt220, vt510 manual says vt102 */ |
1287 | compatibility(VT102); |
374330e2 |
1288 | insch (def(esc_args[0], 1)); |
cabfd08c |
1289 | seen_disp_event = TRUE; |
374330e2 |
1290 | break; |
1291 | case 'P': /* delete chars */ |
e14a5a13 |
1292 | compatibility(VT102); |
374330e2 |
1293 | insch (-def(esc_args[0], 1)); |
cabfd08c |
1294 | seen_disp_event = TRUE; |
374330e2 |
1295 | break; |
1296 | case 'c': /* terminal type query */ |
e14a5a13 |
1297 | compatibility(VT100); |
1298 | /* This is the response for a VT102 */ |
0965bee0 |
1299 | ldisc_send (id_string, strlen(id_string)); |
374330e2 |
1300 | break; |
1301 | case 'n': /* cursor position query */ |
1302 | if (esc_args[0] == 6) { |
1303 | char buf[32]; |
1304 | sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1); |
0965bee0 |
1305 | ldisc_send (buf, strlen(buf)); |
374330e2 |
1306 | } |
e14a5a13 |
1307 | else if (esc_args[0] == 5) { |
0965bee0 |
1308 | ldisc_send ("\033[0n", 4); |
e14a5a13 |
1309 | } |
374330e2 |
1310 | break; |
e14a5a13 |
1311 | case 'h': /* toggle modes to high */ |
8b811b12 |
1312 | case ANSI_QUE('h'): |
e14a5a13 |
1313 | compatibility(VT100); |
1314 | { |
1315 | int i; |
1316 | for (i=0; i<esc_nargs; i++) |
1317 | toggle_mode (esc_args[i], esc_query, TRUE); |
1318 | } |
374330e2 |
1319 | break; |
e14a5a13 |
1320 | case 'l': /* toggle modes to low */ |
8b811b12 |
1321 | case ANSI_QUE('l'): |
e14a5a13 |
1322 | compatibility(VT100); |
1323 | { |
1324 | int i; |
1325 | for (i=0; i<esc_nargs; i++) |
1326 | toggle_mode (esc_args[i], esc_query, FALSE); |
1327 | } |
374330e2 |
1328 | break; |
1329 | case 'g': /* clear tabs */ |
e14a5a13 |
1330 | compatibility(VT100); |
374330e2 |
1331 | if (esc_nargs == 1) { |
1332 | if (esc_args[0] == 0) { |
1333 | tabs[curs_x] = FALSE; |
1334 | } else if (esc_args[0] == 3) { |
1335 | int i; |
1336 | for (i = 0; i < cols; i++) |
1337 | tabs[i] = FALSE; |
1338 | } |
1339 | } |
1340 | break; |
1341 | case 'r': /* set scroll margins */ |
e14a5a13 |
1342 | compatibility(VT100); |
8b811b12 |
1343 | if (esc_nargs <= 2) { |
374330e2 |
1344 | int top, bot; |
7e394092 |
1345 | top = def(esc_args[0], 1) - 1; |
1346 | bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows : |
1347 | def(esc_args[1], rows)) - 1; |
374330e2 |
1348 | if (bot >= rows) |
1349 | bot = rows-1; |
7e394092 |
1350 | /* VTTEST Bug 9 - if region is less than 2 lines |
1351 | * don't change region. |
1352 | */ |
83ea993b |
1353 | if (bot-top > 0) { |
374330e2 |
1354 | marg_t = top; |
1355 | marg_b = bot; |
1356 | curs_x = 0; |
1357 | /* |
1358 | * I used to think the cursor should be |
1359 | * placed at the top of the newly marginned |
1360 | * area. Apparently not: VMS TPU falls over |
1361 | * if so. |
e14a5a13 |
1362 | * |
1363 | * Well actually it should for Origin mode - RDB |
374330e2 |
1364 | */ |
e14a5a13 |
1365 | curs_y = (dec_om ? marg_t : 0); |
374330e2 |
1366 | fix_cpos; |
cabfd08c |
1367 | seen_disp_event = TRUE; |
374330e2 |
1368 | } |
1369 | } |
1370 | break; |
1371 | case 'm': /* set graphics rendition */ |
1372 | { |
e14a5a13 |
1373 | /* |
1374 | * A VT100 without the AVO only had one attribute, either |
1375 | * underline or reverse video depending on the cursor type, |
1376 | * this was selected by CSI 7m. |
1377 | * |
1378 | * case 2: |
1379 | * This is DIM on the VT100-AVO and VT102 |
1380 | * case 5: |
1381 | * This is BLINK on the VT100-AVO and VT102+ |
1382 | * case 8: |
1383 | * This is INVIS on the VT100-AVO and VT102 |
1384 | * case 21: |
1385 | * This like 22 disables BOLD, DIM and INVIS |
1386 | * |
1387 | * The ANSI colours appear on any terminal that has colour |
1388 | * (obviously) but the interaction between sgr0 and the |
1389 | * colours varies but is usually related to the background |
1390 | * colour erase item. |
1391 | * The interaction between colour attributes and the mono |
1392 | * ones is also very implementation dependent. |
1393 | * |
1394 | * The 39 and 49 attributes are likely to be unimplemented. |
1395 | */ |
374330e2 |
1396 | int i; |
1397 | for (i=0; i<esc_nargs; i++) { |
1398 | switch (def(esc_args[i], 0)) { |
1399 | case 0: /* restore defaults */ |
1400 | curr_attr = ATTR_DEFAULT; break; |
1401 | case 1: /* enable bold */ |
e14a5a13 |
1402 | compatibility(VT100AVO); |
374330e2 |
1403 | curr_attr |= ATTR_BOLD; break; |
374330e2 |
1404 | case 21: /* (enable double underline) */ |
e14a5a13 |
1405 | compatibility(OTHER); |
1406 | case 4: /* enable underline */ |
1407 | compatibility(VT100AVO); |
374330e2 |
1408 | curr_attr |= ATTR_UNDER; break; |
e14a5a13 |
1409 | case 5: /* enable blink */ |
1410 | compatibility(VT100AVO); |
1411 | curr_attr |= ATTR_BLINK; break; |
374330e2 |
1412 | case 7: /* enable reverse video */ |
1413 | curr_attr |= ATTR_REVERSE; break; |
1414 | case 22: /* disable bold */ |
ec55b220 |
1415 | compatibility2(OTHER,VT220); |
374330e2 |
1416 | curr_attr &= ~ATTR_BOLD; break; |
1417 | case 24: /* disable underline */ |
ec55b220 |
1418 | compatibility2(OTHER,VT220); |
374330e2 |
1419 | curr_attr &= ~ATTR_UNDER; break; |
e14a5a13 |
1420 | case 25: /* disable blink */ |
ec55b220 |
1421 | compatibility2(OTHER,VT220); |
e14a5a13 |
1422 | curr_attr &= ~ATTR_BLINK; break; |
374330e2 |
1423 | case 27: /* disable reverse video */ |
ec55b220 |
1424 | compatibility2(OTHER,VT220); |
374330e2 |
1425 | curr_attr &= ~ATTR_REVERSE; break; |
1426 | case 30: case 31: case 32: case 33: |
1427 | case 34: case 35: case 36: case 37: |
1428 | /* foreground */ |
1429 | curr_attr &= ~ATTR_FGMASK; |
1430 | curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT; |
1431 | break; |
1432 | case 39: /* default-foreground */ |
1433 | curr_attr &= ~ATTR_FGMASK; |
1434 | curr_attr |= ATTR_DEFFG; |
1435 | break; |
1436 | case 40: case 41: case 42: case 43: |
1437 | case 44: case 45: case 46: case 47: |
1438 | /* background */ |
1439 | curr_attr &= ~ATTR_BGMASK; |
1440 | curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT; |
1441 | break; |
1442 | case 49: /* default-background */ |
1443 | curr_attr &= ~ATTR_BGMASK; |
1444 | curr_attr |= ATTR_DEFBG; |
1445 | break; |
1446 | } |
1447 | } |
e14a5a13 |
1448 | if (use_bce) |
c9def1b8 |
1449 | erase_char = |
1450 | (' '| |
1451 | (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK)) |
1452 | ); |
374330e2 |
1453 | } |
1454 | break; |
1455 | case 's': /* save cursor */ |
1456 | save_cursor (TRUE); |
1457 | break; |
1458 | case 'u': /* restore cursor */ |
1459 | save_cursor (FALSE); |
cabfd08c |
1460 | seen_disp_event = TRUE; |
374330e2 |
1461 | break; |
1462 | case 't': /* set page size - ie window height */ |
e14a5a13 |
1463 | /* |
1464 | * VT340/VT420 sequence DECSLPP, DEC only allows values |
1465 | * 24/25/36/48/72/144 other emulators (eg dtterm) use |
1466 | * illegal values (eg first arg 1..9) for window changing |
1467 | * and reports. |
1468 | */ |
1469 | compatibility(VT340TEXT); |
1470 | if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) { |
c9def1b8 |
1471 | request_resize (cols, def(esc_args[0], 24), 0); |
e14a5a13 |
1472 | deselect(); |
1473 | } |
1474 | break; |
1475 | case ANSI('|', '*'): |
1476 | /* VT420 sequence DECSNLS |
1477 | * Set number of lines on screen |
1478 | * VT420 uses VGA like hardware and can support any size in |
1479 | * reasonable range (24..49 AIUI) with no default specified. |
1480 | */ |
1481 | compatibility(VT420); |
c9def1b8 |
1482 | if (esc_nargs==1 && esc_args[0]>0) { |
1483 | request_resize (cols, def(esc_args[0], cfg.height), 0); |
e14a5a13 |
1484 | deselect(); |
1485 | } |
1486 | break; |
1487 | case ANSI('|', '$'): |
1488 | /* VT340/VT420 sequence DECSCPP |
1489 | * Set number of columns per page |
1490 | * Docs imply range is only 80 or 132, but I'll allow any. |
1491 | */ |
1492 | compatibility(VT340TEXT); |
1493 | if (esc_nargs<=1) { |
c9def1b8 |
1494 | request_resize (def(esc_args[0], cfg.width), rows, 0); |
e14a5a13 |
1495 | deselect(); |
1496 | } |
374330e2 |
1497 | break; |
1498 | case 'X': /* write N spaces w/o moving cursor */ |
e14a5a13 |
1499 | /* XXX VTTEST says this is vt220, vt510 manual says vt100 */ |
c9def1b8 |
1500 | compatibility(ANSIMIN); |
374330e2 |
1501 | { |
1502 | int n = def(esc_args[0], 1); |
1503 | unsigned long *p = cpos; |
1504 | if (n > cols - curs_x) |
1505 | n = cols - curs_x; |
1506 | check_selection (cpos, cpos+n); |
1507 | while (n--) |
e14a5a13 |
1508 | *p++ = erase_char; |
cabfd08c |
1509 | seen_disp_event = TRUE; |
374330e2 |
1510 | } |
1511 | break; |
1512 | case 'x': /* report terminal characteristics */ |
e14a5a13 |
1513 | compatibility(VT100); |
374330e2 |
1514 | { |
1515 | char buf[32]; |
1516 | int i = def(esc_args[0], 0); |
1517 | if (i == 0 || i == 1) { |
1518 | strcpy (buf, "\033[2;1;1;112;112;1;0x"); |
1519 | buf[2] += i; |
0965bee0 |
1520 | ldisc_send (buf, 20); |
374330e2 |
1521 | } |
1522 | } |
1523 | break; |
e14a5a13 |
1524 | case ANSI('L','='): |
1525 | compatibility(OTHER); |
1526 | use_bce = (esc_args[0]<=0); |
1527 | erase_char = ERASE_CHAR; |
1528 | if (use_bce) |
1529 | erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
1530 | break; |
c9def1b8 |
1531 | case ANSI('E','='): |
1532 | compatibility(OTHER); |
1533 | blink_is_real = (esc_args[0]>=1); |
1534 | break; |
e14a5a13 |
1535 | case ANSI('p','"'): |
1536 | /* Allow the host to make this emulator a 'perfect' VT102. |
1537 | * This first appeared in the VT220, but we do need to get |
1538 | * back to PuTTY mode so I won't check it. |
1539 | * |
ec55b220 |
1540 | * The arg in 40..42 are a PuTTY extension. |
1541 | * The 2nd arg, 8bit vs 7bit is not checked. |
e14a5a13 |
1542 | * |
1543 | * Setting VT102 mode should also change the Fkeys to |
1544 | * generate PF* codes as a real VT102 has no Fkeys. |
1545 | * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys |
1546 | * send nothing. |
1547 | * |
1548 | * Note ESC c will NOT change this! |
1549 | */ |
1550 | |
ec55b220 |
1551 | switch (esc_args[0]) { |
1552 | case 61: compatibility_level &= ~TM_VTXXX; |
1553 | compatibility_level |= TM_VT102; break; |
1554 | case 62: compatibility_level &= ~TM_VTXXX; |
1555 | compatibility_level |= TM_VT220; break; |
1556 | |
1557 | default: if( esc_args[0] > 60 && esc_args[0] < 70 ) |
1558 | compatibility_level |= TM_VTXXX; |
1559 | break; |
1560 | |
1561 | case 40: compatibility_level &= TM_VTXXX; break; |
1562 | case 41: compatibility_level = TM_PUTTY; break; |
1563 | case 42: compatibility_level = TM_SCOANSI; break; |
1564 | |
1565 | case ARG_DEFAULT: |
1566 | compatibility_level = TM_PUTTY; break; |
1567 | case 50: break; |
1568 | } |
1569 | |
1570 | /* Change the response to CSI c */ |
1571 | if (esc_args[0] == 50) { |
1572 | int i; |
1573 | char lbuf[64]; |
1574 | strcpy(id_string, "\033[?"); |
1575 | for (i=1; i<esc_nargs; i++) { |
1576 | if (i!=1) strcat(id_string, ";"); |
1577 | sprintf(lbuf, "%d", esc_args[i]); |
1578 | strcat(id_string, lbuf); |
1579 | } |
1580 | strcat(id_string, "c"); |
1581 | } |
1582 | |
1583 | #if 0 |
1584 | /* Is this a good idea ? |
1585 | * Well we should do a soft reset at this point ... |
1586 | */ |
1587 | if (!has_compat(VT420) && has_compat(VT100)) { |
1588 | if (reset_132) request_resize (132, 24, 1); |
1589 | else request_resize ( 80, 24, 1); |
1590 | } |
1591 | #endif |
e14a5a13 |
1592 | break; |
374330e2 |
1593 | } |
1594 | break; |
1595 | case SET_GL: |
1596 | case SET_GR: |
e14a5a13 |
1597 | /* VT100 only here, checked above */ |
374330e2 |
1598 | switch (c) { |
1599 | case 'A': |
1600 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR; |
1601 | break; |
1602 | case '0': |
1603 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW; |
1604 | break; |
e14a5a13 |
1605 | case 'B': |
374330e2 |
1606 | default: /* specifically, 'B' */ |
1607 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII; |
1608 | break; |
1609 | } |
e14a5a13 |
1610 | if( !has_compat(VT220) || c != '%' ) |
8b811b12 |
1611 | termstate = TOPLEVEL; |
374330e2 |
1612 | break; |
1613 | case SEEN_OSC: |
1614 | osc_w = FALSE; |
1615 | switch (c) { |
374330e2 |
1616 | case 'P': /* Linux palette sequence */ |
1617 | termstate = SEEN_OSC_P; |
1618 | osc_strlen = 0; |
1619 | break; |
1620 | case 'R': /* Linux palette reset */ |
1621 | palette_reset(); |
1622 | term_invalidate(); |
1623 | termstate = TOPLEVEL; |
1624 | break; |
1625 | case 'W': /* word-set */ |
1626 | termstate = SEEN_OSC_W; |
1627 | osc_w = TRUE; |
1628 | break; |
1629 | case '0': case '1': case '2': case '3': case '4': |
1630 | case '5': case '6': case '7': case '8': case '9': |
1631 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
1632 | break; |
1633 | case 'L': |
1634 | /* |
1635 | * Grotty hack to support xterm and DECterm title |
1636 | * sequences concurrently. |
1637 | */ |
1638 | if (esc_args[0] == 2) { |
1639 | esc_args[0] = 1; |
1640 | break; |
1641 | } |
1642 | /* else fall through */ |
1643 | default: |
1644 | termstate = OSC_STRING; |
1645 | osc_strlen = 0; |
1646 | } |
1647 | break; |
1648 | case OSC_STRING: |
8b811b12 |
1649 | /* |
1650 | * This OSC stuff is EVIL. It takes just one character to get into |
1651 | * sysline mode and it's not initially obvious how to get out. |
1652 | * So I've added CR and LF as string aborts. |
1653 | * This shouldn't effect compatibility as I believe embedded |
1654 | * control characters are supposed to be interpreted (maybe?) |
1655 | * and they don't display anything useful anyway. |
1656 | * |
1657 | * -- RDB |
1658 | */ |
1659 | if (c == '\n' || c == '\r') { |
1660 | termstate = TOPLEVEL; |
1661 | } else if (c == 0234 || c == '\007' ) { |
374330e2 |
1662 | /* |
1663 | * These characters terminate the string; ST and BEL |
1664 | * terminate the sequence and trigger instant |
1665 | * processing of it, whereas ESC goes back to SEEN_ESC |
1666 | * mode unless it is followed by \, in which case it is |
1667 | * synonymous with ST in the first place. |
1668 | */ |
1669 | do_osc(); |
1670 | termstate = TOPLEVEL; |
1671 | } else if (c == '\033') |
1672 | termstate = OSC_MAYBE_ST; |
1673 | else if (osc_strlen < OSC_STR_MAX) |
1674 | osc_string[osc_strlen++] = c; |
1675 | break; |
1676 | case SEEN_OSC_P: |
1677 | { |
1678 | int max = (osc_strlen == 0 ? 21 : 16); |
1679 | int val; |
1680 | if (c >= '0' && c <= '9') |
1681 | val = c - '0'; |
1682 | else if (c >= 'A' && c <= 'A'+max-10) |
1683 | val = c - 'A' + 10; |
1684 | else if (c >= 'a' && c <= 'a'+max-10) |
1685 | val = c - 'a' + 10; |
1686 | else |
1687 | termstate = TOPLEVEL; |
1688 | osc_string[osc_strlen++] = val; |
1689 | if (osc_strlen >= 7) { |
1690 | palette_set (osc_string[0], |
1691 | osc_string[1] * 16 + osc_string[2], |
1692 | osc_string[3] * 16 + osc_string[4], |
1693 | osc_string[5] * 16 + osc_string[6]); |
1694 | term_invalidate(); |
1695 | termstate = TOPLEVEL; |
1696 | } |
1697 | } |
1698 | break; |
1699 | case SEEN_OSC_W: |
1700 | switch (c) { |
374330e2 |
1701 | case '0': case '1': case '2': case '3': case '4': |
1702 | case '5': case '6': case '7': case '8': case '9': |
1703 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
1704 | break; |
1705 | default: |
1706 | termstate = OSC_STRING; |
1707 | osc_strlen = 0; |
1708 | } |
1709 | break; |
1710 | case SEEN_ESCHASH: |
c9def1b8 |
1711 | { |
1712 | unsigned long *p; |
1713 | unsigned long nlattr; |
1714 | int n; |
1715 | |
1716 | switch (c) { |
1717 | case '8': |
1718 | p = scrtop; |
1719 | n = rows * (cols+1); |
1720 | while (n--) |
1721 | *p++ = ATTR_DEFAULT | 'E'; |
1722 | disptop = scrtop; |
1723 | seen_disp_event = TRUE; |
1724 | check_selection (scrtop, scrtop + rows * (cols+1)); |
1725 | break; |
1726 | |
1727 | case '3': nlattr = LATTR_TOP; if(0) { |
1728 | case '4': nlattr = LATTR_BOT; } if(0) { |
1729 | case '5': nlattr = LATTR_NORM; } if(0) { |
1730 | case '6': nlattr = LATTR_WIDE; } |
1731 | |
1732 | p = scrtop + curs_y * (cols+1) + cols; |
1733 | *p &= ~LATTR_MODE; |
1734 | *p |= nlattr; |
1735 | } |
374330e2 |
1736 | } |
1737 | termstate = TOPLEVEL; |
1738 | break; |
e14a5a13 |
1739 | case VT52_ESC: |
1740 | termstate = TOPLEVEL; |
1741 | seen_disp_event = TRUE; |
1742 | switch (c) { |
1743 | case 'A': |
1744 | move (curs_x, curs_y - 1, 1); |
1745 | break; |
1746 | case 'B': |
1747 | move (curs_x, curs_y + 1, 1); |
1748 | break; |
1749 | case 'C': |
1750 | move (curs_x + 1, curs_y, 1); |
1751 | break; |
1752 | case 'D': |
1753 | move (curs_x - 1, curs_y, 1); |
1754 | break; |
1755 | case 'F': |
1756 | cset_attr[cset=0] = ATTR_LINEDRW; |
1757 | break; |
1758 | case 'G': |
1759 | cset_attr[cset=0] = ATTR_ASCII; |
1760 | break; |
1761 | case 'H': |
1762 | move (0, 0, 0); |
1763 | break; |
1764 | case 'I': |
1765 | if (curs_y == 0) |
1766 | scroll (0, rows-1, -1, TRUE); |
1767 | else if (curs_y > 0) |
1768 | curs_y--; |
1769 | fix_cpos; |
1770 | wrapnext = FALSE; |
1771 | break; |
1772 | case 'J': |
1773 | erase_lots(FALSE, FALSE, TRUE); |
1774 | disptop = scrtop; |
1775 | break; |
1776 | case 'K': |
1777 | erase_lots(TRUE, FALSE, TRUE); |
1778 | break; |
1779 | case 'V': |
1780 | /* XXX Print cursor line */ |
1781 | break; |
1782 | case 'W': |
1783 | /* XXX Start controller mode */ |
1784 | break; |
1785 | case 'X': |
1786 | /* XXX Stop controller mode */ |
1787 | break; |
1788 | case 'Y': |
1789 | termstate = VT52_Y1; |
1790 | break; |
1791 | case 'Z': |
0965bee0 |
1792 | ldisc_send ("\033/Z", 3); |
e14a5a13 |
1793 | break; |
1794 | case '=': |
ec55b220 |
1795 | app_keypad_keys = TRUE; |
e14a5a13 |
1796 | break; |
1797 | case '>': |
ec55b220 |
1798 | app_keypad_keys = FALSE; |
e14a5a13 |
1799 | break; |
1800 | case '<': |
1801 | /* XXX This should switch to VT100 mode not current or default |
1802 | * VT mode. But this will only have effect in a VT220+ |
1803 | * emulation. |
1804 | */ |
1805 | vt52_mode = FALSE; |
1806 | break; |
1807 | case '^': |
1808 | /* XXX Enter auto print mode */ |
1809 | break; |
1810 | case '_': |
1811 | /* XXX Exit auto print mode */ |
1812 | break; |
1813 | case ']': |
1814 | /* XXX Print screen */ |
1815 | break; |
1816 | } |
1817 | break; |
1818 | case VT52_Y1: |
1819 | termstate = VT52_Y2; |
1820 | move(curs_x, c-' ', 0); |
1821 | break; |
1822 | case VT52_Y2: |
1823 | termstate = TOPLEVEL; |
1824 | move(c-' ', curs_y, 0); |
1825 | break; |
374330e2 |
1826 | } |
e14a5a13 |
1827 | if (selstate != NO_SELECTION) |
1828 | check_selection (cpos, cpos+1); |
374330e2 |
1829 | } |
c9def1b8 |
1830 | inbuf_head = 0; |
374330e2 |
1831 | } |
1832 | |
1833 | /* |
1834 | * Compare two lines to determine whether they are sufficiently |
1835 | * alike to scroll-optimise one to the other. Return the degree of |
1836 | * similarity. |
1837 | */ |
1838 | static int linecmp (unsigned long *a, unsigned long *b) { |
1839 | int i, n; |
1840 | |
1841 | for (i=n=0; i < cols; i++) |
1842 | n += (*a++ == *b++); |
1843 | return n; |
1844 | } |
1845 | |
1846 | /* |
1847 | * Given a context, update the window. Out of paranoia, we don't |
1848 | * allow WM_PAINT responses to do scrolling optimisations. |
1849 | */ |
ec8679e9 |
1850 | static void do_paint (Context ctx, int may_optimise){ |
374330e2 |
1851 | int i, j, start, our_curs_y; |
1852 | unsigned long attr, rv, cursor; |
1853 | char ch[1024]; |
156686ef |
1854 | long ticks; |
374330e2 |
1855 | |
156686ef |
1856 | /* |
1857 | * Check the visual bell state. |
1858 | */ |
1859 | if (in_vbell) { |
1860 | ticks = GetTickCount(); |
1861 | if (ticks - vbell_timeout >= 0) |
1862 | in_vbell = FALSE; |
1863 | } |
1864 | |
1865 | /* Depends on: |
1866 | * screen array, disptop, scrtop, |
1867 | * selection, rv, |
1868 | * cfg.blinkpc, blink_is_real, tblinker, |
1869 | * curs_y, curs_x, blinker, cfg.blink_cur, cursor_on, has_focus |
1870 | */ |
e14a5a13 |
1871 | if (cursor_on) { |
1872 | if (has_focus) { |
217dceef |
1873 | if (blinker || !cfg.blink_cur) |
1874 | cursor = ATTR_ACTCURS; |
1875 | else |
1876 | cursor = 0; |
e14a5a13 |
1877 | } |
1878 | else |
1879 | cursor = ATTR_PASCURS; |
4e30ff69 |
1880 | if (wrapnext) |
1881 | cursor |= ATTR_RIGHTCURS; |
e14a5a13 |
1882 | } |
156686ef |
1883 | else |
1884 | cursor = 0; |
1885 | rv = (!rvideo ^ !in_vbell ? ATTR_REVERSE : 0); |
374330e2 |
1886 | our_curs_y = curs_y + (scrtop - disptop) / (cols+1); |
1887 | |
1888 | for (i=0; i<rows; i++) { |
1889 | int idx = i*(cols+1); |
c9def1b8 |
1890 | int lattr = (disptop[idx+cols] & LATTR_MODE); |
374330e2 |
1891 | for (j=0; j<=cols; j++,idx++) { |
1892 | unsigned long *d = disptop+idx; |
4e30ff69 |
1893 | wanttext[idx] = lattr | (((*d &~ ATTR_WRAPPED) ^ rv |
374330e2 |
1894 | ^ (selstart <= d && d < selend ? |
1895 | ATTR_REVERSE : 0)) | |
1896 | (i==our_curs_y && j==curs_x ? cursor : 0)); |
c9def1b8 |
1897 | if (blink_is_real) { |
1898 | if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) ) |
1899 | { |
1900 | wanttext[idx] &= ATTR_MASK; |
1901 | wanttext[idx] += ' '; |
1902 | } |
1903 | wanttext[idx] &= ~ATTR_BLINK; |
1904 | } |
374330e2 |
1905 | } |
1906 | } |
1907 | |
1908 | /* |
1909 | * We would perform scrolling optimisations in here, if they |
1910 | * didn't have a nasty tendency to cause the whole sodding |
1911 | * program to hang for a second at speed-critical moments. |
1912 | * We'll leave it well alone... |
1913 | */ |
1914 | |
1915 | for (i=0; i<rows; i++) { |
1916 | int idx = i*(cols+1); |
c9def1b8 |
1917 | int lattr = (wanttext[idx+cols] & LATTR_MODE); |
374330e2 |
1918 | start = -1; |
1919 | for (j=0; j<=cols; j++,idx++) { |
1920 | unsigned long t = wanttext[idx]; |
1921 | int needs_update = (j < cols && t != disptext[idx]); |
1922 | int keep_going = (start != -1 && needs_update && |
1923 | (t & ATTR_MASK) == attr && |
1924 | j-start < sizeof(ch)); |
1925 | if (start != -1 && !keep_going) { |
c9def1b8 |
1926 | do_text (ctx, start, i, ch, j-start, attr, lattr); |
374330e2 |
1927 | start = -1; |
1928 | } |
1929 | if (needs_update) { |
1930 | if (start == -1) { |
1931 | start = j; |
1932 | attr = t & ATTR_MASK; |
1933 | } |
1934 | ch[j-start] = (char) (t & CHAR_MASK); |
1935 | } |
1936 | disptext[idx] = t; |
1937 | } |
1938 | } |
1939 | } |
1940 | |
1941 | /* |
e14a5a13 |
1942 | * Flick the switch that says if blinking things should be shown or hidden. |
1943 | */ |
1944 | |
1945 | void term_blink(int flg) { |
22dcdc3b |
1946 | static long last_blink = 0; |
1947 | static long last_tblink = 0; |
e14a5a13 |
1948 | long now, blink_diff; |
1949 | |
c9def1b8 |
1950 | now = GetTickCount(); |
1951 | blink_diff = now-last_tblink; |
1952 | |
1953 | /* Make sure the text blinks no more than 2Hz */ |
1954 | if (blink_diff<0 || blink_diff>450) |
1955 | { |
1956 | last_tblink = now; |
1957 | tblinker = !tblinker; |
1958 | } |
1959 | |
e14a5a13 |
1960 | if (flg) { |
1961 | blinker = 1; |
c9def1b8 |
1962 | last_blink = now; |
e14a5a13 |
1963 | return; |
1964 | } |
e14a5a13 |
1965 | |
1966 | blink_diff = now-last_blink; |
1967 | |
22dcdc3b |
1968 | /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */ |
1969 | if (blink_diff>=0 && blink_diff<(long)GetCaretBlinkTime()) |
e14a5a13 |
1970 | return; |
1971 | |
1972 | last_blink = now; |
1973 | blinker = !blinker; |
1974 | } |
1975 | |
1976 | /* |
374330e2 |
1977 | * Invalidate the whole screen so it will be repainted in full. |
1978 | */ |
1979 | void term_invalidate(void) { |
1980 | int i; |
1981 | |
1982 | for (i=0; i<rows*(cols+1); i++) |
1983 | disptext[i] = ATTR_INVALID; |
1984 | } |
1985 | |
1986 | /* |
1987 | * Paint the window in response to a WM_PAINT message. |
1988 | */ |
1989 | void term_paint (Context ctx, int l, int t, int r, int b) { |
1990 | int i, j, left, top, right, bottom; |
1991 | |
1992 | left = l / font_width; |
1993 | right = (r - 1) / font_width; |
1994 | top = t / font_height; |
1995 | bottom = (b - 1) / font_height; |
e42ad027 |
1996 | for (i = top; i <= bottom && i < rows ; i++) |
c9def1b8 |
1997 | { |
1998 | if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM) |
1999 | for (j = left; j <= right && j < cols ; j++) |
2000 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
2001 | else |
2002 | for (j = left/2; j <= right/2+1 && j < cols ; j++) |
2003 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
2004 | } |
374330e2 |
2005 | |
e14a5a13 |
2006 | /* This should happen soon enough, also for some reason it sometimes |
2007 | * fails to actually do anything when re-sizing ... painting the wrong |
2008 | * window perhaps ? |
374330e2 |
2009 | do_paint (ctx, FALSE); |
e14a5a13 |
2010 | */ |
374330e2 |
2011 | } |
2012 | |
2013 | /* |
2014 | * Attempt to scroll the scrollback. The second parameter gives the |
2015 | * position we want to scroll to; the first is +1 to denote that |
2016 | * this position is relative to the beginning of the scrollback, -1 |
2017 | * to denote it is relative to the end, and 0 to denote that it is |
2018 | * relative to the current position. |
2019 | */ |
2020 | void term_scroll (int rel, int where) { |
2021 | int n = where * (cols+1); |
2022 | |
2023 | disptop = (rel < 0 ? scrtop : |
2024 | rel > 0 ? sbtop : disptop) + n; |
2025 | if (disptop < sbtop) |
2026 | disptop = sbtop; |
2027 | if (disptop > scrtop) |
2028 | disptop = scrtop; |
2029 | update_sbar(); |
2030 | term_update(); |
2031 | } |
2032 | |
508a6084 |
2033 | static void clipme(unsigned long *top, unsigned long *bottom, char *workbuf) { |
bc1235d4 |
2034 | char *wbptr; /* where next char goes within workbuf */ |
2035 | int wblen = 0; /* workbuf len */ |
2036 | int buflen; /* amount of memory allocated to workbuf */ |
2037 | |
2038 | if ( workbuf != NULL ) { /* user supplied buffer? */ |
2039 | buflen = -1; /* assume buffer passed in is big enough */ |
2040 | wbptr = workbuf; /* start filling here */ |
2041 | } |
2042 | else |
2043 | buflen = 0; /* No data is available yet */ |
2044 | |
2045 | while (top < bottom) { |
2046 | int nl = FALSE; |
2047 | unsigned long *lineend = top - (top-text) % (cols+1) + cols; |
2048 | unsigned long *nlpos = lineend; |
2049 | |
2050 | if (!(*nlpos & ATTR_WRAPPED)) { |
2051 | while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > top) |
2052 | nlpos--; |
2053 | if (nlpos < bottom) |
2054 | nl = TRUE; |
2055 | } |
2056 | while (top < nlpos && top < bottom) |
2057 | { |
d3a22f79 |
2058 | int ch = (*top & CHAR_MASK); |
2059 | int set = (*top & CSET_MASK); |
2060 | |
2061 | /* VT Specials -> ISO8859-1 for Cut&Paste */ |
2062 | static const unsigned char poorman2[] = |
bc1235d4 |
2063 | "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 "; |
bc1235d4 |
2064 | |
d3a22f79 |
2065 | if (set && !cfg.rawcnp) { |
2066 | if (set == ATTR_LINEDRW && ch >= 0x60 && ch < 0x7F) { |
2067 | int x; |
2068 | if ((x = poorman2[2*(ch-0x60)+1]) == ' ') |
2069 | x = 0; |
2070 | ch = (x<<8) + poorman2[2*(ch-0x60)]; |
2071 | } |
2072 | } |
bc1235d4 |
2073 | |
d3a22f79 |
2074 | while(ch != 0) { |
2075 | if (cfg.rawcnp || !!(ch&0xE0)) { |
2076 | if ( wblen == buflen ) |
2077 | { |
2078 | workbuf = srealloc(workbuf, buflen += 100); |
2079 | wbptr = workbuf + wblen; |
2080 | } |
2081 | wblen++; |
2082 | *wbptr++ = (unsigned char) ch; |
2083 | } |
2084 | ch>>=8; |
bc1235d4 |
2085 | } |
bc1235d4 |
2086 | top++; |
2087 | } |
2088 | if (nl) { |
2089 | int i; |
2090 | for (i=0; i<sizeof(sel_nl); i++) |
2091 | { |
2092 | if ( wblen == buflen ) |
2093 | { |
2094 | workbuf = srealloc(workbuf, buflen += 100); |
2095 | wbptr = workbuf + wblen; |
2096 | } |
2097 | wblen++; |
2098 | *wbptr++ = sel_nl[i]; |
2099 | } |
2100 | } |
2101 | top = lineend + 1; /* start of next line */ |
2102 | } |
2103 | write_clip (workbuf, wblen, FALSE); /* transfer to clipboard */ |
2104 | if ( buflen > 0 ) /* indicates we allocated this buffer */ |
2105 | sfree(workbuf); |
2106 | |
2107 | } |
2108 | void term_copyall (void) { |
2109 | clipme(sbtop, cpos, NULL /* dynamic allocation */); |
2110 | } |
2111 | |
374330e2 |
2112 | /* |
2113 | * Spread the selection outwards according to the selection mode. |
2114 | */ |
2115 | static unsigned long *sel_spread_half (unsigned long *p, int dir) { |
2116 | unsigned long *linestart, *lineend; |
2117 | int x; |
2118 | short wvalue; |
2119 | |
2120 | x = (p - text) % (cols+1); |
2121 | linestart = p - x; |
2122 | lineend = linestart + cols; |
2123 | |
2124 | switch (selmode) { |
2125 | case SM_CHAR: |
2126 | /* |
2127 | * In this mode, every character is a separate unit, except |
2128 | * for runs of spaces at the end of a non-wrapping line. |
2129 | */ |
2130 | if (!(linestart[cols] & ATTR_WRAPPED)) { |
2131 | unsigned long *q = lineend; |
2132 | while (q > linestart && (q[-1] & CHAR_MASK) == 0x20) |
2133 | q--; |
2134 | if (q == lineend) |
2135 | q--; |
2136 | if (p >= q) |
2137 | p = (dir == -1 ? q : lineend - 1); |
2138 | } |
2139 | break; |
2140 | case SM_WORD: |
2141 | /* |
2142 | * In this mode, the units are maximal runs of characters |
2143 | * whose `wordness' has the same value. |
2144 | */ |
2145 | wvalue = wordness[*p & CHAR_MASK]; |
2146 | if (dir == +1) { |
2147 | while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue) |
2148 | p++; |
2149 | } else { |
2150 | while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue) |
2151 | p--; |
2152 | } |
2153 | break; |
2154 | case SM_LINE: |
2155 | /* |
2156 | * In this mode, every line is a unit. |
2157 | */ |
2158 | p = (dir == -1 ? linestart : lineend - 1); |
2159 | break; |
2160 | } |
2161 | return p; |
2162 | } |
2163 | |
2164 | static void sel_spread (void) { |
2165 | selstart = sel_spread_half (selstart, -1); |
2166 | selend = sel_spread_half (selend - 1, +1) + 1; |
2167 | } |
2168 | |
2169 | void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) { |
37508af4 |
2170 | unsigned long *selpoint; |
2171 | |
2172 | if (y<0) y = 0; |
2173 | if (y>=rows) y = rows-1; |
094ed2a6 |
2174 | if (x<0) { |
2175 | if (y > 0) { |
2176 | x = cols-1; |
2177 | y--; |
2178 | } else |
2179 | x = 0; |
2180 | } |
37508af4 |
2181 | if (x>=cols) x = cols-1; |
2182 | |
c9def1b8 |
2183 | selpoint = disptop + y * (cols+1); |
2184 | if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM) |
2185 | selpoint += x/2; |
2186 | else |
2187 | selpoint += x; |
374330e2 |
2188 | |
2189 | if (b == MB_SELECT && a == MA_CLICK) { |
2190 | deselect(); |
2191 | selstate = ABOUT_TO; |
2192 | selanchor = selpoint; |
2193 | selmode = SM_CHAR; |
2194 | } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) { |
2195 | deselect(); |
2196 | selmode = (a == MA_2CLK ? SM_WORD : SM_LINE); |
2197 | selstate = DRAGGING; |
2198 | selstart = selanchor = selpoint; |
2199 | selend = selstart + 1; |
2200 | sel_spread(); |
2201 | } else if ((b == MB_SELECT && a == MA_DRAG) || |
2202 | (b == MB_EXTEND && a != MA_RELEASE)) { |
2203 | if (selstate == ABOUT_TO && selanchor == selpoint) |
2204 | return; |
2205 | if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) { |
2206 | if (selpoint-selstart < (selend-selstart)/2) |
2207 | selanchor = selend - 1; |
2208 | else |
2209 | selanchor = selstart; |
2210 | selstate = DRAGGING; |
2211 | } |
2212 | if (selstate != ABOUT_TO && selstate != DRAGGING) |
2213 | selanchor = selpoint; |
2214 | selstate = DRAGGING; |
2215 | if (selpoint < selanchor) { |
2216 | selstart = selpoint; |
2217 | selend = selanchor + 1; |
2218 | } else { |
2219 | selstart = selanchor; |
2220 | selend = selpoint + 1; |
2221 | } |
2222 | sel_spread(); |
2223 | } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) { |
2224 | if (selstate == DRAGGING) { |
2225 | /* |
2226 | * We've completed a selection. We now transfer the |
2227 | * data to the clipboard. |
2228 | */ |
bc1235d4 |
2229 | clipme(selstart, selend, selspace); |
374330e2 |
2230 | selstate = SELECTED; |
2231 | } else |
2232 | selstate = NO_SELECTION; |
2233 | } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) { |
2234 | char *data; |
2235 | int len; |
2236 | |
2237 | get_clip((void **) &data, &len); |
2238 | if (data) { |
2239 | char *p, *q; |
c9def1b8 |
2240 | |
2241 | if (paste_buffer) sfree(paste_buffer); |
2242 | paste_pos = paste_hold = paste_len = 0; |
2243 | paste_buffer = smalloc(len); |
2244 | |
374330e2 |
2245 | p = q = data; |
2246 | while (p < data+len) { |
2247 | while (p < data+len && |
2248 | !(p <= data+len-sizeof(sel_nl) && |
2249 | !memcmp(p, sel_nl, sizeof(sel_nl)))) |
2250 | p++; |
14963b8f |
2251 | |
2252 | { |
2253 | int i; |
2254 | unsigned char c; |
2255 | for(i=0;i<p-q;i++) |
2256 | { |
2257 | c=xlat_kbd2tty(q[i]); |
c9def1b8 |
2258 | paste_buffer[paste_len++] = c; |
14963b8f |
2259 | } |
2260 | } |
2261 | |
374330e2 |
2262 | if (p <= data+len-sizeof(sel_nl) && |
2263 | !memcmp(p, sel_nl, sizeof(sel_nl))) { |
c9def1b8 |
2264 | paste_buffer[paste_len++] = '\r'; |
374330e2 |
2265 | p += sizeof(sel_nl); |
2266 | } |
2267 | q = p; |
2268 | } |
c9def1b8 |
2269 | |
2270 | /* Assume a small paste will be OK in one go. */ |
2271 | if (paste_len<256) { |
0965bee0 |
2272 | ldisc_send (paste_buffer, paste_len); |
c9def1b8 |
2273 | if (paste_buffer) sfree(paste_buffer); |
2274 | paste_buffer = 0; |
2275 | paste_pos = paste_hold = paste_len = 0; |
2276 | } |
374330e2 |
2277 | } |
2278 | get_clip(NULL, NULL); |
2279 | } |
2280 | |
2281 | term_update(); |
2282 | } |
2283 | |
c9def1b8 |
2284 | void term_nopaste() { |
2285 | if(paste_len == 0) return; |
2286 | sfree(paste_buffer); |
2287 | paste_buffer = 0; |
2288 | paste_len = 0; |
2289 | } |
2290 | |
2291 | void term_paste() { |
8df7a775 |
2292 | static long last_paste = 0; |
c9def1b8 |
2293 | long now, paste_diff; |
2294 | |
2295 | if(paste_len == 0) return; |
2296 | |
2297 | /* Don't wait forever to paste */ |
2298 | if(paste_hold) { |
2299 | now = GetTickCount(); |
2300 | paste_diff = now-last_paste; |
2301 | if (paste_diff>=0 && paste_diff<450) |
2302 | return; |
2303 | } |
2304 | paste_hold = 0; |
2305 | |
2306 | while(paste_pos<paste_len) |
2307 | { |
8df7a775 |
2308 | int n = 0; |
2309 | while (n + paste_pos < paste_len) { |
2310 | if (paste_buffer[paste_pos + n++] == '\r') |
2311 | break; |
2312 | } |
0965bee0 |
2313 | ldisc_send (paste_buffer+paste_pos, n); |
8df7a775 |
2314 | paste_pos += n; |
c9def1b8 |
2315 | |
8df7a775 |
2316 | if (paste_pos < paste_len) { |
c9def1b8 |
2317 | paste_hold = 1; |
2318 | return; |
2319 | } |
2320 | } |
2321 | sfree(paste_buffer); |
2322 | paste_buffer = 0; |
2323 | paste_len = 0; |
2324 | } |
2325 | |
374330e2 |
2326 | static void deselect (void) { |
2327 | selstate = NO_SELECTION; |
2328 | selstart = selend = scrtop; |
2329 | } |
2330 | |
2331 | void term_deselect (void) { |
2332 | deselect(); |
2333 | term_update(); |
2334 | } |
fe50e814 |
2335 | |
0965bee0 |
2336 | int term_ldisc(int option) { |
2337 | if (option == LD_ECHO) return term_echoing; |
2338 | if (option == LD_EDIT) return term_editing; |
2339 | return FALSE; |
2340 | } |
2341 | |
fe50e814 |
2342 | /* |
2343 | * from_backend(), to get data from the backend for the terminal. |
2344 | */ |
2345 | void from_backend(int is_stderr, char *data, int len) { |
2346 | while (len--) { |
2347 | if (inbuf_head >= INBUF_SIZE) |
2348 | term_out(); |
2349 | inbuf[inbuf_head++] = *data++; |
2350 | } |
2351 | } |
e1c8e0ed |
2352 | |
2353 | /* |
2354 | * Log session traffic. |
2355 | */ |
2356 | void logtraffic(unsigned char c, int logmode) { |
2357 | if (cfg.logtype > 0) { |
2358 | if (cfg.logtype == logmode) { |
2359 | /* deferred open file from pgm start? */ |
2360 | if (!lgfp) logfopen(); |
2361 | if (lgfp) fputc (c, lgfp); |
2362 | } |
2363 | } |
2364 | } |
2365 | |
2366 | /* open log file append/overwrite mode */ |
2367 | void logfopen(void) { |
2368 | char buf[256]; |
2369 | time_t t; |
2370 | struct tm *tm; |
2371 | char writemod[4]; |
2372 | |
2373 | if (!cfg.logtype) |
2374 | return; |
2375 | sprintf (writemod, "wb"); /* default to rewrite */ |
2376 | lgfp = fopen(cfg.logfilename, "r"); /* file already present? */ |
2377 | if (lgfp) { |
2378 | int i; |
2379 | fclose(lgfp); |
2380 | i = askappend(cfg.logfilename); |
2381 | if (i == 1) |
2382 | writemod[0] = 'a'; /* set append mode */ |
2383 | else if (i == 0) { /* cancelled */ |
2384 | lgfp = NULL; |
e4b0ba16 |
2385 | cfg.logtype = 0; /* disable logging */ |
e1c8e0ed |
2386 | return; |
2387 | } |
2388 | } |
2389 | |
2390 | lgfp = fopen(cfg.logfilename, writemod); |
2391 | if (lgfp) { /* enter into event log */ |
2392 | sprintf(buf, "%s session log (%s mode) to file : ", |
2393 | (writemod[0] == 'a') ? "Appending" : "Writing new", |
2394 | (cfg.logtype == LGTYP_ASCII ? "ASCII" : |
2395 | cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>") ); |
2396 | /* Make sure we do not exceed the output buffer size */ |
2397 | strncat (buf, cfg.logfilename, 128); |
2398 | buf[strlen(buf)] = '\0'; |
2399 | logevent(buf); |
2400 | |
2401 | /* --- write header line iinto log file */ |
2402 | fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp); |
2403 | time(&t); |
2404 | tm = localtime(&t); |
2405 | strftime(buf, 24, "%Y.%m.%d %H:%M:%S", tm); |
2406 | fputs (buf, lgfp); |
2407 | fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp); |
2408 | } |
2409 | } |
2410 | |
2411 | void logfclose (void) { |
2412 | if (lgfp) { fclose(lgfp); lgfp = NULL; } |
2413 | } |