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