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