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