1 /**************************************************************************** 2 * Copyright 2018-2019,2020 Thomas E. Dickey * 3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. * 4 * * 5 * Permission is hereby granted, free of charge, to any person obtaining a * 6 * copy of this software and associated documentation files (the * 7 * "Software"), to deal in the Software without restriction, including * 8 * without limitation the rights to use, copy, modify, merge, publish, * 9 * distribute, distribute with modifications, sublicense, and/or sell * 10 * copies of the Software, and to permit persons to whom the Software is * 11 * furnished to do so, subject to the following conditions: * 12 * * 13 * The above copyright notice and this permission notice shall be included * 14 * in all copies or substantial portions of the Software. * 15 * * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 23 * * 24 * Except as contained in this notice, the name(s) of the above copyright * 25 * holders shall not be used in advertising or otherwise to promote the * 26 * sale, use or other dealings in this Software without prior written * 27 * authorization. * 28 ****************************************************************************/ 29 30 /**************************************************************************** 31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 32 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 33 * and: Thomas E. Dickey 1996-on * 34 * and: Juergen Pfeifer 2009 * 35 ****************************************************************************/ 36 37 /* 38 ** lib_mvcur.c 39 ** 40 ** The routines for moving the physical cursor and scrolling: 41 ** 42 ** void _nc_mvcur_init(void) 43 ** 44 ** void _nc_mvcur_resume(void) 45 ** 46 ** int mvcur(int old_y, int old_x, int new_y, int new_x) 47 ** 48 ** void _nc_mvcur_wrap(void) 49 ** 50 ** Comparisons with older movement optimizers: 51 ** SVr3 curses mvcur() can't use cursor_to_ll or auto_left_margin. 52 ** 4.4BSD curses can't use cuu/cud/cuf/cub/hpa/vpa/tab/cbt for local 53 ** motions. It doesn't use tactics based on auto_left_margin. Weirdly 54 ** enough, it doesn't use its own hardware-scrolling routine to scroll up 55 ** destination lines for out-of-bounds addresses! 56 ** old ncurses optimizer: less accurate cost computations (in fact, 57 ** it was broken and had to be commented out!). 58 ** 59 ** Compile with -DMAIN to build an interactive tester/timer for the movement 60 ** optimizer. You can use it to investigate the optimizer's behavior. 61 ** You can also use it for tuning the formulas used to determine whether 62 ** or not full optimization is attempted. 63 ** 64 ** This code has a nasty tendency to find bugs in terminfo entries, because it 65 ** exercises the non-cup movement capabilities heavily. If you think you've 66 ** found a bug, try deleting subsets of the following capabilities (arranged 67 ** in decreasing order of suspiciousness): it, tab, cbt, hpa, vpa, cuu, cud, 68 ** cuf, cub, cuu1, cud1, cuf1, cub1. It may be that one or more are wrong. 69 ** 70 ** Note: you should expect this code to look like a resource hog in a profile. 71 ** That's because it does a lot of I/O, through the tputs() calls. The I/O 72 ** cost swamps the computation overhead (and as machines get faster, this 73 ** will become even more true). Comments in the test exerciser at the end 74 ** go into detail about tuning and how you can gauge the optimizer's 75 ** effectiveness. 76 **/ 77 78 /**************************************************************************** 79 * 80 * Constants and macros for optimizer tuning. 81 * 82 ****************************************************************************/ 83 84 /* 85 * The average overhead of a full optimization computation in character 86 * transmission times. If it's too high, the algorithm will be a bit 87 * over-biased toward using cup rather than local motions; if it's too 88 * low, the algorithm may spend more time than is strictly optimal 89 * looking for non-cup motions. Profile the optimizer using the `t' 90 * command of the exerciser (see below), and round to the nearest integer. 91 * 92 * Yes, I (esr) thought about computing expected overhead dynamically, say 93 * by derivation from a running average of optimizer times. But the 94 * whole point of this optimization is to *decrease* the frequency of 95 * system calls. :-) 96 */ 97 #define COMPUTE_OVERHEAD 1 /* I use a 90MHz Pentium @ 9.6Kbps */ 98 99 /* 100 * LONG_DIST is the distance we consider to be just as costly to move over as a 101 * cup sequence is to emit. In other words, it's the length of a cup sequence 102 * adjusted for average computation overhead. The magic number is the length 103 * of "\033[yy;xxH", the typical cup sequence these days. 104 */ 105 #define LONG_DIST (8 - COMPUTE_OVERHEAD) 106 107 /* 108 * Tell whether a motion is optimizable by local motions. Needs to be cheap to 109 * compute. In general, all the fast moves go to either the right or left edge 110 * of the screen. So any motion to a location that is (a) further away than 111 * LONG_DIST and (b) further inward from the right or left edge than LONG_DIST, 112 * we'll consider nonlocal. 113 */ 114 #define NOT_LOCAL(sp, fy, fx, ty, tx) ((tx > LONG_DIST) \ 115 && (tx < screen_columns(sp) - 1 - LONG_DIST) \ 116 && (abs(ty-fy) + abs(tx-fx) > LONG_DIST)) 117 118 /**************************************************************************** 119 * 120 * External interfaces 121 * 122 ****************************************************************************/ 123 124 /* 125 * For this code to work OK, the following components must live in the 126 * screen structure: 127 * 128 * int _char_padding; // cost of character put 129 * int _cr_cost; // cost of (carriage_return) 130 * int _cup_cost; // cost of (cursor_address) 131 * int _home_cost; // cost of (cursor_home) 132 * int _ll_cost; // cost of (cursor_to_ll) 133 *#if USE_HARD_TABS 134 * int _ht_cost; // cost of (tab) 135 * int _cbt_cost; // cost of (back_tab) 136 *#endif USE_HARD_TABS 137 * int _cub1_cost; // cost of (cursor_left) 138 * int _cuf1_cost; // cost of (cursor_right) 139 * int _cud1_cost; // cost of (cursor_down) 140 * int _cuu1_cost; // cost of (cursor_up) 141 * int _cub_cost; // cost of (parm_cursor_left) 142 * int _cuf_cost; // cost of (parm_cursor_right) 143 * int _cud_cost; // cost of (parm_cursor_down) 144 * int _cuu_cost; // cost of (parm_cursor_up) 145 * int _hpa_cost; // cost of (column_address) 146 * int _vpa_cost; // cost of (row_address) 147 * int _ech_cost; // cost of (erase_chars) 148 * int _rep_cost; // cost of (repeat_char) 149 * 150 * The USE_HARD_TABS switch controls whether it is reliable to use tab/backtabs 151 * for local motions. On many systems, it's not, due to uncertainties about 152 * tab delays and whether or not tabs will be expanded in raw mode. If you 153 * have parm_right_cursor, tab motions don't win you a lot anyhow. 154 */ 155 156 #include <curses.priv.h> 157 #include <ctype.h> 158 159 #ifndef CUR 160 #define CUR SP_TERMTYPE 161 #endif 162 163 MODULE_ID("$Id: lib_mvcur.c,v 1.151 2020/02/02 23:34:34 tom Exp $") 164 165 #define WANT_CHAR(sp, y, x) NewScreen(sp)->_line[y].text[x] /* desired state */ 166 167 #if NCURSES_SP_FUNCS 168 #define BAUDRATE(sp) sp->_term->_baudrate /* bits per second */ 169 #else 170 #define BAUDRATE(sp) cur_term->_baudrate /* bits per second */ 171 #endif 172 173 #if defined(MAIN) || defined(NCURSES_TEST) 174 #include <sys/time.h> 175 176 static bool profiling = FALSE; 177 static float diff; 178 #endif /* MAIN */ 179 180 #undef NCURSES_OUTC_FUNC 181 #define NCURSES_OUTC_FUNC myOutCh 182 183 #define OPT_SIZE 512 184 185 static int normalized_cost(NCURSES_SP_DCLx const char *const cap, int affcnt); 186 187 /**************************************************************************** 188 * 189 * Initialization/wrapup (including cost pre-computation) 190 * 191 ****************************************************************************/ 192 193 #ifdef TRACE 194 static int 195 trace_cost_of(NCURSES_SP_DCLx const char *capname, const char *cap, int affcnt) 196 { 197 int result = NCURSES_SP_NAME(_nc_msec_cost) (NCURSES_SP_ARGx cap, affcnt); 198 TR(TRACE_CHARPUT | TRACE_MOVE, 199 ("CostOf %s %d %s", capname, result, _nc_visbuf(cap))); 200 return result; 201 } 202 #define CostOf(cap,affcnt) trace_cost_of(NCURSES_SP_ARGx #cap, cap, affcnt) 203 204 static int 205 trace_normalized_cost(NCURSES_SP_DCLx const char *capname, const char *cap, int affcnt) 206 { 207 int result = normalized_cost(NCURSES_SP_ARGx cap, affcnt); 208 TR(TRACE_CHARPUT | TRACE_MOVE, 209 ("NormalizedCost %s %d %s", capname, result, _nc_visbuf(cap))); 210 return result; 211 } 212 #define NormalizedCost(cap,affcnt) trace_normalized_cost(NCURSES_SP_ARGx #cap, cap, affcnt) 213 214 #else 215 216 #define CostOf(cap,affcnt) NCURSES_SP_NAME(_nc_msec_cost)(NCURSES_SP_ARGx cap, affcnt) 217 #define NormalizedCost(cap,affcnt) normalized_cost(NCURSES_SP_ARGx cap, affcnt) 218 219 #endif 220 221 NCURSES_EXPORT(int) 222 NCURSES_SP_NAME(_nc_msec_cost) (NCURSES_SP_DCLx const char *const cap, int affcnt) 223 /* compute the cost of a given operation */ 224 { 225 if (cap == 0) 226 return (INFINITY); 227 else { 228 const char *cp; 229 float cum_cost = 0.0; 230 231 for (cp = cap; *cp; cp++) { 232 /* extract padding, either mandatory or required */ 233 if (cp[0] == '$' && cp[1] == '<' && strchr(cp, '>')) { 234 float number = 0.0; 235 236 for (cp += 2; *cp != '>'; cp++) { 237 if (isdigit(UChar(*cp))) 238 number = number * 10 + (float) (*cp - '0'); 239 else if (*cp == '*') 240 number *= (float) affcnt; 241 else if (*cp == '.' && (*++cp != '>') && isdigit(UChar(*cp))) 242 number += (float) ((*cp - '0') / 10.0); 243 } 244 245 #if NCURSES_NO_PADDING 246 if (!GetNoPadding(SP_PARM)) 247 #endif 248 cum_cost += number * 10; 249 } else if (SP_PARM) { 250 cum_cost += (float) SP_PARM->_char_padding; 251 } 252 } 253 254 return ((int) cum_cost); 255 } 256 } 257 258 #if NCURSES_SP_FUNCS 259 NCURSES_EXPORT(int) 260 _nc_msec_cost(const char *const cap, int affcnt) 261 { 262 return NCURSES_SP_NAME(_nc_msec_cost) (CURRENT_SCREEN, cap, affcnt); 263 } 264 #endif 265 266 static int 267 normalized_cost(NCURSES_SP_DCLx const char *const cap, int affcnt) 268 /* compute the effective character-count for an operation (round up) */ 269 { 270 int cost = NCURSES_SP_NAME(_nc_msec_cost) (NCURSES_SP_ARGx cap, affcnt); 271 if (cost != INFINITY) 272 cost = (cost + SP_PARM->_char_padding - 1) / SP_PARM->_char_padding; 273 return cost; 274 } 275 276 static void 277 reset_scroll_region(NCURSES_SP_DCL0) 278 /* Set the scroll-region to a known state (the default) */ 279 { 280 if (change_scroll_region) { 281 NCURSES_PUTP2("change_scroll_region", 282 TPARM_2(change_scroll_region, 283 0, screen_lines(SP_PARM) - 1)); 284 } 285 } 286 287 NCURSES_EXPORT(void) 288 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_DCL0) 289 /* what to do at initialization time and after each shellout */ 290 { 291 if (!SP_PARM || !IsTermInfo(SP_PARM)) 292 return; 293 294 /* initialize screen for cursor access */ 295 if (enter_ca_mode) { 296 NCURSES_PUTP2("enter_ca_mode", enter_ca_mode); 297 } 298 299 /* 300 * Doing this here rather than in _nc_mvcur_wrap() ensures that 301 * ncurses programs will see a reset scroll region even if a 302 * program that messed with it died ungracefully. 303 * 304 * This also undoes the effects of terminal init strings that assume 305 * they know the screen size. This is useful when you're running 306 * a vt100 emulation through xterm. 307 */ 308 reset_scroll_region(NCURSES_SP_ARG); 309 SP_PARM->_cursrow = SP_PARM->_curscol = -1; 310 311 /* restore cursor shape */ 312 if (SP_PARM->_cursor != -1) { 313 int cursor = SP_PARM->_cursor; 314 SP_PARM->_cursor = -1; 315 NCURSES_SP_NAME(curs_set) (NCURSES_SP_ARGx cursor); 316 } 317 } 318 319 #if NCURSES_SP_FUNCS 320 NCURSES_EXPORT(void) 321 _nc_mvcur_resume(void) 322 { 323 NCURSES_SP_NAME(_nc_mvcur_resume) (CURRENT_SCREEN); 324 } 325 #endif 326 327 NCURSES_EXPORT(void) 328 NCURSES_SP_NAME(_nc_mvcur_init) (NCURSES_SP_DCL0) 329 /* initialize the cost structure */ 330 { 331 if (SP_PARM->_ofp && NC_ISATTY(fileno(SP_PARM->_ofp))) { 332 SP_PARM->_char_padding = ((BAUDBYTE * 1000 * 10) 333 / (BAUDRATE(SP_PARM) > 0 334 ? BAUDRATE(SP_PARM) 335 : 9600)); 336 } else { 337 SP_PARM->_char_padding = 1; /* must be nonzero */ 338 } 339 if (SP_PARM->_char_padding <= 0) 340 SP_PARM->_char_padding = 1; /* must be nonzero */ 341 TR(TRACE_CHARPUT | TRACE_MOVE, ("char_padding %d msecs", SP_PARM->_char_padding)); 342 343 /* non-parameterized local-motion strings */ 344 SP_PARM->_cr_cost = CostOf(carriage_return, 0); 345 SP_PARM->_home_cost = CostOf(cursor_home, 0); 346 SP_PARM->_ll_cost = CostOf(cursor_to_ll, 0); 347 #if USE_HARD_TABS 348 if (getenv("NCURSES_NO_HARD_TABS") == 0 349 && dest_tabs_magic_smso == 0 350 && HasHardTabs()) { 351 SP_PARM->_ht_cost = CostOf(tab, 0); 352 SP_PARM->_cbt_cost = CostOf(back_tab, 0); 353 } else { 354 SP_PARM->_ht_cost = INFINITY; 355 SP_PARM->_cbt_cost = INFINITY; 356 } 357 #endif /* USE_HARD_TABS */ 358 SP_PARM->_cub1_cost = CostOf(cursor_left, 0); 359 SP_PARM->_cuf1_cost = CostOf(cursor_right, 0); 360 SP_PARM->_cud1_cost = CostOf(cursor_down, 0); 361 SP_PARM->_cuu1_cost = CostOf(cursor_up, 0); 362 363 SP_PARM->_smir_cost = CostOf(enter_insert_mode, 0); 364 SP_PARM->_rmir_cost = CostOf(exit_insert_mode, 0); 365 SP_PARM->_ip_cost = 0; 366 if (insert_padding) { 367 SP_PARM->_ip_cost = CostOf(insert_padding, 0); 368 } 369 370 /* 371 * Assumption: if the terminal has memory_relative addressing, the 372 * initialization strings or smcup will set single-page mode so we 373 * can treat it like absolute screen addressing. This seems to be true 374 * for all cursor_mem_address terminal types in the terminfo database. 375 */ 376 SP_PARM->_address_cursor = cursor_address ? cursor_address : cursor_mem_address; 377 378 /* 379 * Parametrized local-motion strings. This static cost computation 380 * depends on the following assumptions: 381 * 382 * (1) They never have * padding. In the entire master terminfo database 383 * as of March 1995, only the obsolete Zenith Z-100 pc violates this. 384 * (Proportional padding is found mainly in insert, delete and scroll 385 * capabilities). 386 * 387 * (2) The average case of cup has two two-digit parameters. Strictly, 388 * the average case for a 24 * 80 screen has ((10*10*(1 + 1)) + 389 * (14*10*(1 + 2)) + (10*70*(2 + 1)) + (14*70*4)) / (24*80) = 3.458 390 * digits of parameters. On a 25x80 screen the average is 3.6197. 391 * On larger screens the value gets much closer to 4. 392 * 393 * (3) The average case of cub/cuf/hpa/ech/rep has 2 digits of parameters 394 * (strictly, (((10 * 1) + (70 * 2)) / 80) = 1.8750). 395 * 396 * (4) The average case of cud/cuu/vpa has 2 digits of parameters 397 * (strictly, (((10 * 1) + (14 * 2)) / 24) = 1.5833). 398 * 399 * All these averages depend on the assumption that all parameter values 400 * are equally probable. 401 */ 402 SP_PARM->_cup_cost = CostOf(TPARM_2(SP_PARM->_address_cursor, 23, 23), 1); 403 SP_PARM->_cub_cost = CostOf(TPARM_1(parm_left_cursor, 23), 1); 404 SP_PARM->_cuf_cost = CostOf(TPARM_1(parm_right_cursor, 23), 1); 405 SP_PARM->_cud_cost = CostOf(TPARM_1(parm_down_cursor, 23), 1); 406 SP_PARM->_cuu_cost = CostOf(TPARM_1(parm_up_cursor, 23), 1); 407 SP_PARM->_hpa_cost = CostOf(TPARM_1(column_address, 23), 1); 408 SP_PARM->_vpa_cost = CostOf(TPARM_1(row_address, 23), 1); 409 410 /* non-parameterized screen-update strings */ 411 SP_PARM->_ed_cost = NormalizedCost(clr_eos, 1); 412 SP_PARM->_el_cost = NormalizedCost(clr_eol, 1); 413 SP_PARM->_el1_cost = NormalizedCost(clr_bol, 1); 414 SP_PARM->_dch1_cost = NormalizedCost(delete_character, 1); 415 SP_PARM->_ich1_cost = NormalizedCost(insert_character, 1); 416 417 /* 418 * If this is a bce-terminal, we want to bias the choice so we use clr_eol 419 * rather than spaces at the end of a line. 420 */ 421 if (back_color_erase) 422 SP_PARM->_el_cost = 0; 423 424 /* parameterized screen-update strings */ 425 SP_PARM->_dch_cost = NormalizedCost(TPARM_1(parm_dch, 23), 1); 426 SP_PARM->_ich_cost = NormalizedCost(TPARM_1(parm_ich, 23), 1); 427 SP_PARM->_ech_cost = NormalizedCost(TPARM_1(erase_chars, 23), 1); 428 SP_PARM->_rep_cost = NormalizedCost(TPARM_2(repeat_char, ' ', 23), 1); 429 430 SP_PARM->_cup_ch_cost = NormalizedCost( 431 TPARM_2(SP_PARM->_address_cursor, 432 23, 23), 433 1); 434 SP_PARM->_hpa_ch_cost = NormalizedCost(TPARM_1(column_address, 23), 1); 435 SP_PARM->_cuf_ch_cost = NormalizedCost(TPARM_1(parm_right_cursor, 23), 1); 436 SP_PARM->_inline_cost = min(SP_PARM->_cup_ch_cost, 437 min(SP_PARM->_hpa_ch_cost, 438 SP_PARM->_cuf_ch_cost)); 439 440 /* 441 * If save_cursor is used within enter_ca_mode, we should not use it for 442 * scrolling optimization, since the corresponding restore_cursor is not 443 * nested on the various terminals (vt100, xterm, etc.) which use this 444 * feature. 445 */ 446 if (save_cursor != 0 447 && enter_ca_mode != 0 448 && strstr(enter_ca_mode, save_cursor) != 0) { 449 T(("...suppressed sc/rc capability due to conflict with smcup/rmcup")); 450 save_cursor = 0; 451 restore_cursor = 0; 452 } 453 454 /* 455 * A different, possibly better way to arrange this would be to set the 456 * SCREEN's _endwin at window initialization time and let this be called by 457 * doupdate's return-from-shellout code. 458 */ 459 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG); 460 } 461 462 #if NCURSES_SP_FUNCS 463 NCURSES_EXPORT(void) 464 _nc_mvcur_init(void) 465 { 466 NCURSES_SP_NAME(_nc_mvcur_init) (CURRENT_SCREEN); 467 } 468 #endif 469 470 NCURSES_EXPORT(void) 471 NCURSES_SP_NAME(_nc_mvcur_wrap) (NCURSES_SP_DCL0) 472 /* wrap up cursor-addressing mode */ 473 { 474 /* leave cursor at screen bottom */ 475 TINFO_MVCUR(NCURSES_SP_ARGx -1, -1, screen_lines(SP_PARM) - 1, 0); 476 477 if (!SP_PARM || !IsTermInfo(SP_PARM)) 478 return; 479 480 /* set cursor to normal mode */ 481 if (SP_PARM->_cursor != -1) { 482 int cursor = SP_PARM->_cursor; 483 NCURSES_SP_NAME(curs_set) (NCURSES_SP_ARGx 1); 484 SP_PARM->_cursor = cursor; 485 } 486 487 if (exit_ca_mode) { 488 NCURSES_PUTP2("exit_ca_mode", exit_ca_mode); 489 } 490 /* 491 * Reset terminal's tab counter. There's a long-time bug that 492 * if you exit a "curses" program such as vi or more, tab 493 * forward, and then backspace, the cursor doesn't go to the 494 * right place. The problem is that the kernel counts the 495 * escape sequences that reset things as column positions. 496 * Utter a \r to reset this invisibly. 497 */ 498 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_ARGx '\r'); 499 } 500 501 #if NCURSES_SP_FUNCS 502 NCURSES_EXPORT(void) 503 _nc_mvcur_wrap(void) 504 { 505 NCURSES_SP_NAME(_nc_mvcur_wrap) (CURRENT_SCREEN); 506 } 507 #endif 508 509 /**************************************************************************** 510 * 511 * Optimized cursor movement 512 * 513 ****************************************************************************/ 514 515 /* 516 * Perform repeated-append, returning cost 517 */ 518 static NCURSES_INLINE int 519 repeated_append(string_desc * target, int total, int num, int repeat, const char *src) 520 { 521 size_t need = (size_t) repeat * strlen(src); 522 523 if (need < target->s_size) { 524 while (repeat-- > 0) { 525 if (_nc_safe_strcat(target, src)) { 526 total += num; 527 } else { 528 total = INFINITY; 529 break; 530 } 531 } 532 } else { 533 total = INFINITY; 534 } 535 return total; 536 } 537 538 #ifndef NO_OPTIMIZE 539 #define NEXTTAB(fr) (fr + init_tabs - (fr % init_tabs)) 540 541 /* 542 * Assume back_tab (CBT) does not wrap backwards at the left margin, return 543 * a negative value at that point to simplify the loop. 544 */ 545 #define LASTTAB(fr) ((fr > 0) ? ((fr - 1) / init_tabs) * init_tabs : -1) 546 547 static int 548 relative_move(NCURSES_SP_DCLx 549 string_desc * target, 550 int from_y, 551 int from_x, 552 int to_y, 553 int to_x, 554 int ovw) 555 /* move via local motions (cuu/cuu1/cud/cud1/cub1/cub/cuf1/cuf/vpa/hpa) */ 556 { 557 string_desc save; 558 int n, vcost = 0, hcost = 0; 559 560 (void) _nc_str_copy(&save, target); 561 562 if (to_y != from_y) { 563 vcost = INFINITY; 564 565 if (row_address != 0 566 && _nc_safe_strcat(target, TPARM_1(row_address, to_y))) { 567 vcost = SP_PARM->_vpa_cost; 568 } 569 570 if (to_y > from_y) { 571 n = (to_y - from_y); 572 573 if (parm_down_cursor 574 && SP_PARM->_cud_cost < vcost 575 && _nc_safe_strcat(_nc_str_copy(target, &save), 576 TPARM_1(parm_down_cursor, n))) { 577 vcost = SP_PARM->_cud_cost; 578 } 579 580 if (cursor_down 581 && (*cursor_down != '\n' || SP_PARM->_nl) 582 && (n * SP_PARM->_cud1_cost < vcost)) { 583 vcost = repeated_append(_nc_str_copy(target, &save), 0, 584 SP_PARM->_cud1_cost, n, cursor_down); 585 } 586 } else { /* (to_y < from_y) */ 587 n = (from_y - to_y); 588 589 if (parm_up_cursor 590 && SP_PARM->_cuu_cost < vcost 591 && _nc_safe_strcat(_nc_str_copy(target, &save), 592 TPARM_1(parm_up_cursor, n))) { 593 vcost = SP_PARM->_cuu_cost; 594 } 595 596 if (cursor_up && (n * SP_PARM->_cuu1_cost < vcost)) { 597 vcost = repeated_append(_nc_str_copy(target, &save), 0, 598 SP_PARM->_cuu1_cost, n, cursor_up); 599 } 600 } 601 602 if (vcost == INFINITY) 603 return (INFINITY); 604 } 605 606 save = *target; 607 608 if (to_x != from_x) { 609 char str[OPT_SIZE]; 610 string_desc check; 611 612 hcost = INFINITY; 613 614 if (column_address 615 && _nc_safe_strcat(_nc_str_copy(target, &save), 616 TPARM_1(column_address, to_x))) { 617 hcost = SP_PARM->_hpa_cost; 618 } 619 620 if (to_x > from_x) { 621 n = to_x - from_x; 622 623 if (parm_right_cursor 624 && SP_PARM->_cuf_cost < hcost 625 && _nc_safe_strcat(_nc_str_copy(target, &save), 626 TPARM_1(parm_right_cursor, n))) { 627 hcost = SP_PARM->_cuf_cost; 628 } 629 630 if (cursor_right) { 631 int lhcost = 0; 632 633 (void) _nc_str_init(&check, str, sizeof(str)); 634 635 #if USE_HARD_TABS 636 /* use hard tabs, if we have them, to do as much as possible */ 637 if (init_tabs > 0 && tab) { 638 int nxt, fr; 639 640 for (fr = from_x; (nxt = NEXTTAB(fr)) <= to_x; fr = nxt) { 641 lhcost = repeated_append(&check, lhcost, 642 SP_PARM->_ht_cost, 1, tab); 643 if (lhcost == INFINITY) 644 break; 645 } 646 647 n = to_x - fr; 648 from_x = fr; 649 } 650 #endif /* USE_HARD_TABS */ 651 652 if (n <= 0 || n >= (int) check.s_size) 653 ovw = FALSE; 654 #if BSD_TPUTS 655 /* 656 * If we're allowing BSD-style padding in tputs, don't generate 657 * a string with a leading digit. Otherwise, that will be 658 * interpreted as a padding value rather than sent to the 659 * screen. 660 */ 661 if (ovw 662 && n > 0 663 && n < (int) check.s_size 664 && vcost == 0 665 && str[0] == '\0') { 666 int wanted = CharOf(WANT_CHAR(SP_PARM, to_y, from_x)); 667 if (is8bits(wanted) && isdigit(wanted)) 668 ovw = FALSE; 669 } 670 #endif 671 /* 672 * If we have no attribute changes, overwrite is cheaper. 673 * Note: must suppress this by passing in ovw = FALSE whenever 674 * WANT_CHAR would return invalid data. In particular, this 675 * is true between the time a hardware scroll has been done 676 * and the time the structure WANT_CHAR would access has been 677 * updated. 678 */ 679 if (ovw) { 680 int i; 681 682 for (i = 0; i < n; i++) { 683 NCURSES_CH_T ch = WANT_CHAR(SP_PARM, to_y, from_x + i); 684 if (!SameAttrOf(ch, SCREEN_ATTRS(SP_PARM)) 685 #if USE_WIDEC_SUPPORT 686 || !Charable(ch) 687 #endif 688 ) { 689 ovw = FALSE; 690 break; 691 } 692 } 693 } 694 if (ovw) { 695 int i; 696 697 for (i = 0; i < n; i++) 698 *check.s_tail++ = (char) CharOf(WANT_CHAR(SP_PARM, to_y, 699 from_x + i)); 700 *check.s_tail = '\0'; 701 check.s_size -= (size_t) n; 702 lhcost += n * SP_PARM->_char_padding; 703 } else { 704 lhcost = repeated_append(&check, lhcost, SP_PARM->_cuf1_cost, 705 n, cursor_right); 706 } 707 708 if (lhcost < hcost 709 && _nc_safe_strcat(_nc_str_copy(target, &save), str)) { 710 hcost = lhcost; 711 } 712 } 713 } else { /* (to_x < from_x) */ 714 n = from_x - to_x; 715 716 if (parm_left_cursor 717 && SP_PARM->_cub_cost < hcost 718 && _nc_safe_strcat(_nc_str_copy(target, &save), 719 TPARM_1(parm_left_cursor, n))) { 720 hcost = SP_PARM->_cub_cost; 721 } 722 723 if (cursor_left) { 724 int lhcost = 0; 725 726 (void) _nc_str_init(&check, str, sizeof(str)); 727 728 #if USE_HARD_TABS 729 if (init_tabs > 0 && back_tab) { 730 int nxt, fr; 731 732 for (fr = from_x; (nxt = LASTTAB(fr)) >= to_x; fr = nxt) { 733 lhcost = repeated_append(&check, lhcost, 734 SP_PARM->_cbt_cost, 735 1, back_tab); 736 if (lhcost == INFINITY) 737 break; 738 } 739 740 n = fr - to_x; 741 } 742 #endif /* USE_HARD_TABS */ 743 744 lhcost = repeated_append(&check, lhcost, 745 SP_PARM->_cub1_cost, 746 n, cursor_left); 747 748 if (lhcost < hcost 749 && _nc_safe_strcat(_nc_str_copy(target, &save), str)) { 750 hcost = lhcost; 751 } 752 } 753 } 754 755 if (hcost == INFINITY) 756 return (INFINITY); 757 } 758 759 return (vcost + hcost); 760 } 761 #endif /* !NO_OPTIMIZE */ 762 763 /* 764 * With the machinery set up above, it's conceivable that 765 * onscreen_mvcur could be modified into a recursive function that does 766 * an alpha-beta search of motion space, as though it were a chess 767 * move tree, with the weight function being boolean and the search 768 * depth equated to length of string. However, this would jack up the 769 * computation cost a lot, especially on terminals without a cup 770 * capability constraining the search tree depth. So we settle for 771 * the simpler method below. 772 */ 773 774 static NCURSES_INLINE int 775 onscreen_mvcur(NCURSES_SP_DCLx 776 int yold, int xold, 777 int ynew, int xnew, int ovw, 778 NCURSES_SP_OUTC myOutCh) 779 /* onscreen move from (yold, xold) to (ynew, xnew) */ 780 { 781 string_desc result; 782 char buffer[OPT_SIZE]; 783 int tactic = 0, newcost, usecost = INFINITY; 784 int t5_cr_cost; 785 786 #if defined(MAIN) || defined(NCURSES_TEST) 787 struct timeval before, after; 788 789 gettimeofday(&before, NULL); 790 #endif /* MAIN */ 791 792 #define NullResult _nc_str_null(&result, sizeof(buffer)) 793 #define InitResult _nc_str_init(&result, buffer, sizeof(buffer)) 794 795 /* tactic #0: use direct cursor addressing */ 796 if (_nc_safe_strcpy(InitResult, TPARM_2(SP_PARM->_address_cursor, ynew, xnew))) { 797 tactic = 0; 798 usecost = SP_PARM->_cup_cost; 799 800 #if defined(TRACE) || defined(NCURSES_TEST) 801 if (!(_nc_optimize_enable & OPTIMIZE_MVCUR)) 802 goto nonlocal; 803 #endif /* TRACE */ 804 805 /* 806 * We may be able to tell in advance that the full optimization 807 * will probably not be worth its overhead. Also, don't try to 808 * use local movement if the current attribute is anything but 809 * A_NORMAL...there are just too many ways this can screw up 810 * (like, say, local-movement \n getting mapped to some obscure 811 * character because A_ALTCHARSET is on). 812 */ 813 if (yold == -1 || xold == -1 || NOT_LOCAL(SP_PARM, yold, xold, ynew, xnew)) { 814 #if defined(MAIN) || defined(NCURSES_TEST) 815 if (!profiling) { 816 (void) fputs("nonlocal\n", stderr); 817 goto nonlocal; /* always run the optimizer if profiling */ 818 } 819 #else 820 goto nonlocal; 821 #endif /* MAIN */ 822 } 823 } 824 #ifndef NO_OPTIMIZE 825 /* tactic #1: use local movement */ 826 if (yold != -1 && xold != -1 827 && ((newcost = relative_move(NCURSES_SP_ARGx 828 NullResult, 829 yold, xold, 830 ynew, xnew, ovw)) != INFINITY) 831 && newcost < usecost) { 832 tactic = 1; 833 usecost = newcost; 834 } 835 836 /* tactic #2: use carriage-return + local movement */ 837 if (yold != -1 && carriage_return 838 && ((newcost = relative_move(NCURSES_SP_ARGx 839 NullResult, 840 yold, 0, 841 ynew, xnew, ovw)) != INFINITY) 842 && SP_PARM->_cr_cost + newcost < usecost) { 843 tactic = 2; 844 usecost = SP_PARM->_cr_cost + newcost; 845 } 846 847 /* tactic #3: use home-cursor + local movement */ 848 if (cursor_home 849 && ((newcost = relative_move(NCURSES_SP_ARGx 850 NullResult, 851 0, 0, 852 ynew, xnew, ovw)) != INFINITY) 853 && SP_PARM->_home_cost + newcost < usecost) { 854 tactic = 3; 855 usecost = SP_PARM->_home_cost + newcost; 856 } 857 858 /* tactic #4: use home-down + local movement */ 859 if (cursor_to_ll 860 && ((newcost = relative_move(NCURSES_SP_ARGx 861 NullResult, 862 screen_lines(SP_PARM) - 1, 0, 863 ynew, xnew, ovw)) != INFINITY) 864 && SP_PARM->_ll_cost + newcost < usecost) { 865 tactic = 4; 866 usecost = SP_PARM->_ll_cost + newcost; 867 } 868 869 /* 870 * tactic #5: use left margin for wrap to right-hand side, 871 * unless strange wrap behavior indicated by xenl might hose us. 872 */ 873 t5_cr_cost = (xold > 0 ? SP_PARM->_cr_cost : 0); 874 if (auto_left_margin && !eat_newline_glitch 875 && yold > 0 && cursor_left 876 && ((newcost = relative_move(NCURSES_SP_ARGx 877 NullResult, 878 yold - 1, screen_columns(SP_PARM) - 1, 879 ynew, xnew, ovw)) != INFINITY) 880 && t5_cr_cost + SP_PARM->_cub1_cost + newcost < usecost) { 881 tactic = 5; 882 usecost = t5_cr_cost + SP_PARM->_cub1_cost + newcost; 883 } 884 885 /* 886 * These cases are ordered by estimated relative frequency. 887 */ 888 if (tactic) 889 InitResult; 890 switch (tactic) { 891 case 1: 892 (void) relative_move(NCURSES_SP_ARGx 893 &result, 894 yold, xold, 895 ynew, xnew, ovw); 896 break; 897 case 2: 898 (void) _nc_safe_strcpy(&result, carriage_return); 899 (void) relative_move(NCURSES_SP_ARGx 900 &result, 901 yold, 0, 902 ynew, xnew, ovw); 903 break; 904 case 3: 905 (void) _nc_safe_strcpy(&result, cursor_home); 906 (void) relative_move(NCURSES_SP_ARGx 907 &result, 0, 0, 908 ynew, xnew, ovw); 909 break; 910 case 4: 911 (void) _nc_safe_strcpy(&result, cursor_to_ll); 912 (void) relative_move(NCURSES_SP_ARGx 913 &result, 914 screen_lines(SP_PARM) - 1, 0, 915 ynew, xnew, ovw); 916 break; 917 case 5: 918 if (xold > 0) 919 (void) _nc_safe_strcat(&result, carriage_return); 920 (void) _nc_safe_strcat(&result, cursor_left); 921 (void) relative_move(NCURSES_SP_ARGx 922 &result, 923 yold - 1, screen_columns(SP_PARM) - 1, 924 ynew, xnew, ovw); 925 break; 926 } 927 #endif /* !NO_OPTIMIZE */ 928 929 nonlocal: 930 #if defined(MAIN) || defined(NCURSES_TEST) 931 gettimeofday(&after, NULL); 932 diff = after.tv_usec - before.tv_usec 933 + (after.tv_sec - before.tv_sec) * 1000000; 934 if (!profiling) 935 (void) fprintf(stderr, 936 "onscreen: %d microsec, %f 28.8Kbps char-equivalents\n", 937 (int) diff, diff / 288); 938 #endif /* MAIN */ 939 940 if (usecost != INFINITY) { 941 TR(TRACE_MOVE, ("mvcur tactic %d", tactic)); 942 TPUTS_TRACE("mvcur"); 943 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx 944 buffer, 1, myOutCh); 945 SP_PARM->_cursrow = ynew; 946 SP_PARM->_curscol = xnew; 947 return (OK); 948 } else 949 return (ERR); 950 } 951 952 /* 953 * optimized cursor move from (yold, xold) to (ynew, xnew) 954 */ 955 static int 956 _nc_real_mvcur(NCURSES_SP_DCLx 957 int yold, int xold, 958 int ynew, int xnew, 959 NCURSES_SP_OUTC myOutCh, 960 int ovw) 961 { 962 NCURSES_CH_T oldattr; 963 int code; 964 965 TR(TRACE_CALLS | TRACE_MOVE, (T_CALLED("_nc_tinfo_mvcur(%p,%d,%d,%d,%d)"), 966 (void *) SP_PARM, yold, xold, ynew, xnew)); 967 968 if (SP_PARM == 0) { 969 code = ERR; 970 } else if (yold == ynew && xold == xnew) { 971 code = OK; 972 } else { 973 974 /* 975 * Most work here is rounding for terminal boundaries getting the 976 * column position implied by wraparound or the lack thereof and 977 * rolling up the screen to get ynew on the screen. 978 */ 979 if (xnew >= screen_columns(SP_PARM)) { 980 ynew += xnew / screen_columns(SP_PARM); 981 xnew %= screen_columns(SP_PARM); 982 } 983 984 /* 985 * Force restore even if msgr is on when we're in an alternate 986 * character set -- these have a strong tendency to screw up the CR & 987 * LF used for local character motions! 988 */ 989 oldattr = SCREEN_ATTRS(SP_PARM); 990 if ((AttrOf(oldattr) & A_ALTCHARSET) 991 || (AttrOf(oldattr) && !move_standout_mode)) { 992 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before move", 993 (unsigned long) AttrOf(oldattr), 994 _traceattr(AttrOf(oldattr)))); 995 VIDPUTS(SP_PARM, A_NORMAL, 0); 996 } 997 998 if (xold >= screen_columns(SP_PARM)) { 999 1000 if (SP_PARM->_nl) { 1001 int l = (xold + 1) / screen_columns(SP_PARM); 1002 1003 yold += l; 1004 if (yold >= screen_lines(SP_PARM)) 1005 l -= (yold - screen_lines(SP_PARM) - 1); 1006 1007 if (l > 0) { 1008 if (carriage_return) { 1009 NCURSES_PUTP2("carriage_return", carriage_return); 1010 } else { 1011 myOutCh(NCURSES_SP_ARGx '\r'); 1012 } 1013 xold = 0; 1014 1015 while (l > 0) { 1016 if (newline) { 1017 NCURSES_PUTP2("newline", newline); 1018 } else { 1019 myOutCh(NCURSES_SP_ARGx '\n'); 1020 } 1021 l--; 1022 } 1023 } 1024 } else { 1025 /* 1026 * If caller set nonl(), we cannot really use newlines to 1027 * position to the next row. 1028 */ 1029 xold = -1; 1030 yold = -1; 1031 } 1032 } 1033 1034 if (yold > screen_lines(SP_PARM) - 1) 1035 yold = screen_lines(SP_PARM) - 1; 1036 if (ynew > screen_lines(SP_PARM) - 1) 1037 ynew = screen_lines(SP_PARM) - 1; 1038 1039 /* destination location is on screen now */ 1040 code = onscreen_mvcur(NCURSES_SP_ARGx yold, xold, ynew, xnew, ovw, myOutCh); 1041 1042 /* 1043 * Restore attributes if we disabled them before moving. 1044 */ 1045 if (!SameAttrOf(oldattr, SCREEN_ATTRS(SP_PARM))) { 1046 TR(TRACE_CHARPUT, ("turning on (%#lx) %s after move", 1047 (unsigned long) AttrOf(oldattr), 1048 _traceattr(AttrOf(oldattr)))); 1049 VIDPUTS(SP_PARM, AttrOf(oldattr), GetPair(oldattr)); 1050 } 1051 } 1052 returnCode(code); 1053 } 1054 1055 /* 1056 * These entrypoints are used within the library. 1057 */ 1058 NCURSES_EXPORT(int) 1059 NCURSES_SP_NAME(_nc_mvcur) (NCURSES_SP_DCLx 1060 int yold, int xold, 1061 int ynew, int xnew) 1062 { 1063 int rc; 1064 rc = _nc_real_mvcur(NCURSES_SP_ARGx yold, xold, ynew, xnew, 1065 NCURSES_SP_NAME(_nc_outch), 1066 TRUE); 1067 /* 1068 * With the terminal-driver, we cannot distinguish between internal and 1069 * external calls. Flush the output if the screen has not been 1070 * initialized, e.g., when used from low-level terminfo programs. 1071 */ 1072 if ((SP_PARM != 0) && (SP_PARM->_endwin == ewInitial)) 1073 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG); 1074 return rc; 1075 } 1076 1077 #if NCURSES_SP_FUNCS 1078 NCURSES_EXPORT(int) 1079 _nc_mvcur(int yold, int xold, 1080 int ynew, int xnew) 1081 { 1082 return NCURSES_SP_NAME(_nc_mvcur) (CURRENT_SCREEN, yold, xold, ynew, xnew); 1083 } 1084 #endif 1085 1086 #if defined(USE_TERM_DRIVER) 1087 /* 1088 * The terminal driver does not support the external "mvcur()". 1089 */ 1090 NCURSES_EXPORT(int) 1091 TINFO_MVCUR(NCURSES_SP_DCLx int yold, int xold, int ynew, int xnew) 1092 { 1093 int rc; 1094 rc = _nc_real_mvcur(NCURSES_SP_ARGx 1095 yold, xold, 1096 ynew, xnew, 1097 NCURSES_SP_NAME(_nc_outch), 1098 TRUE); 1099 if ((SP_PARM != 0) && (SP_PARM->_endwin == ewInitial)) 1100 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG); 1101 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG); 1102 return rc; 1103 } 1104 1105 #else /* !USE_TERM_DRIVER */ 1106 1107 /* 1108 * These entrypoints support users of the library. 1109 */ 1110 NCURSES_EXPORT(int) 1111 NCURSES_SP_NAME(mvcur) (NCURSES_SP_DCLx int yold, int xold, int ynew, 1112 int xnew) 1113 { 1114 return _nc_real_mvcur(NCURSES_SP_ARGx 1115 yold, xold, 1116 ynew, xnew, 1117 NCURSES_SP_NAME(_nc_putchar), 1118 FALSE); 1119 } 1120 1121 #if NCURSES_SP_FUNCS 1122 NCURSES_EXPORT(int) 1123 mvcur(int yold, int xold, int ynew, int xnew) 1124 { 1125 return NCURSES_SP_NAME(mvcur) (CURRENT_SCREEN, yold, xold, ynew, xnew); 1126 } 1127 #endif 1128 #endif /* USE_TERM_DRIVER */ 1129 1130 #if defined(TRACE) || defined(NCURSES_TEST) 1131 NCURSES_EXPORT_VAR(int) _nc_optimize_enable = OPTIMIZE_ALL; 1132 #endif 1133 1134 #if defined(MAIN) || defined(NCURSES_TEST) 1135 /**************************************************************************** 1136 * 1137 * Movement optimizer test code 1138 * 1139 ****************************************************************************/ 1140 1141 #include <tic.h> 1142 #include <dump_entry.h> 1143 #include <time.h> 1144 1145 NCURSES_EXPORT_VAR(const char *) _nc_progname = "mvcur"; 1146 1147 static unsigned long xmits; 1148 1149 /* these override lib_tputs.c */ 1150 NCURSES_EXPORT(int) 1151 tputs(const char *string, int affcnt GCC_UNUSED, int (*outc) (int) GCC_UNUSED) 1152 /* stub tputs() that dumps sequences in a visible form */ 1153 { 1154 if (profiling) 1155 xmits += strlen(string); 1156 else 1157 (void) fputs(_nc_visbuf(string), stdout); 1158 return (OK); 1159 } 1160 1161 NCURSES_EXPORT(int) 1162 putp(const char *string) 1163 { 1164 return (tputs(string, 1, _nc_outch)); 1165 } 1166 1167 NCURSES_EXPORT(int) 1168 _nc_outch(int ch) 1169 { 1170 putc(ch, stdout); 1171 return OK; 1172 } 1173 1174 NCURSES_EXPORT(int) 1175 delay_output(int ms GCC_UNUSED) 1176 { 1177 return OK; 1178 } 1179 1180 static char tname[PATH_MAX]; 1181 1182 static void 1183 load_term(void) 1184 { 1185 (void) setupterm(tname, STDOUT_FILENO, NULL); 1186 } 1187 1188 static int 1189 roll(int n) 1190 { 1191 int i, j; 1192 1193 i = (RAND_MAX / n) * n; 1194 while ((j = rand()) >= i) 1195 continue; 1196 return (j % n); 1197 } 1198 1199 int 1200 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) 1201 { 1202 _nc_STRCPY(tname, getenv("TERM"), sizeof(tname)); 1203 load_term(); 1204 _nc_setupscreen(lines, columns, stdout, FALSE, 0); 1205 baudrate(); 1206 1207 _nc_mvcur_init(); 1208 1209 (void) puts("The mvcur tester. Type ? for help"); 1210 1211 fputs("smcup:", stdout); 1212 putchar('\n'); 1213 1214 for (;;) { 1215 int fy, fx, ty, tx, n, i; 1216 char buf[BUFSIZ], capname[BUFSIZ]; 1217 1218 if (fputs("> ", stdout) == EOF) 1219 break; 1220 if (fgets(buf, sizeof(buf), stdin) == 0) 1221 break; 1222 1223 #define PUTS(s) (void) puts(s) 1224 #define PUTF(s,t) (void) printf(s,t) 1225 if (buf[0] == '?') { 1226 PUTS("? -- display this help message"); 1227 PUTS("fy fx ty tx -- (4 numbers) display (fy,fx)->(ty,tx) move"); 1228 PUTS("s[croll] n t b m -- display scrolling sequence"); 1229 PUTF("r[eload] -- reload terminal info for %s\n", 1230 termname()); 1231 PUTS("l[oad] <term> -- load terminal info for type <term>"); 1232 PUTS("d[elete] <cap> -- delete named capability"); 1233 PUTS("i[nspect] -- display terminal capabilities"); 1234 PUTS("c[ost] -- dump cursor-optimization cost table"); 1235 PUTS("o[optimize] -- toggle movement optimization"); 1236 PUTS("t[orture] <num> -- torture-test with <num> random moves"); 1237 PUTS("q[uit] -- quit the program"); 1238 } else if (sscanf(buf, "%d %d %d %d", &fy, &fx, &ty, &tx) == 4) { 1239 struct timeval before, after; 1240 1241 putchar('"'); 1242 1243 gettimeofday(&before, NULL); 1244 mvcur(fy, fx, ty, tx); 1245 gettimeofday(&after, NULL); 1246 1247 printf("\" (%ld msec)\n", 1248 (long) (after.tv_usec - before.tv_usec 1249 + (after.tv_sec - before.tv_sec) 1250 * 1000000)); 1251 } else if (sscanf(buf, "s %d %d %d %d", &fy, &fx, &ty, &tx) == 4) { 1252 struct timeval before, after; 1253 1254 putchar('"'); 1255 1256 gettimeofday(&before, NULL); 1257 _nc_scrolln(fy, fx, ty, tx); 1258 gettimeofday(&after, NULL); 1259 1260 printf("\" (%ld msec)\n", 1261 (long) (after.tv_usec - before.tv_usec + (after.tv_sec - 1262 before.tv_sec) 1263 * 1000000)); 1264 } else if (buf[0] == 'r') { 1265 _nc_STRCPY(tname, termname(), sizeof(tname)); 1266 load_term(); 1267 } else if (sscanf(buf, "l %s", tname) == 1) { 1268 load_term(); 1269 } else if (sscanf(buf, "d %s", capname) == 1) { 1270 struct name_table_entry const *np = _nc_find_entry(capname, 1271 _nc_get_hash_table(FALSE)); 1272 1273 if (np == NULL) 1274 (void) printf("No such capability as \"%s\"\n", capname); 1275 else { 1276 switch (np->nte_type) { 1277 case BOOLEAN: 1278 cur_term->type.Booleans[np->nte_index] = FALSE; 1279 (void) 1280 printf("Boolean capability `%s' (%d) turned off.\n", 1281 np->nte_name, np->nte_index); 1282 break; 1283 1284 case NUMBER: 1285 cur_term->type.Numbers[np->nte_index] = ABSENT_NUMERIC; 1286 (void) printf("Number capability `%s' (%d) set to -1.\n", 1287 np->nte_name, np->nte_index); 1288 break; 1289 1290 case STRING: 1291 cur_term->type.Strings[np->nte_index] = ABSENT_STRING; 1292 (void) printf("String capability `%s' (%d) deleted.\n", 1293 np->nte_name, np->nte_index); 1294 break; 1295 } 1296 } 1297 } else if (buf[0] == 'i') { 1298 dump_init(NULL, F_TERMINFO, S_TERMINFO, 1299 FALSE, 70, 0, 0, FALSE, FALSE, 0); 1300 dump_entry(&TerminalType(cur_term), FALSE, TRUE, 0, 0); 1301 putchar('\n'); 1302 } else if (buf[0] == 'o') { 1303 if (_nc_optimize_enable & OPTIMIZE_MVCUR) { 1304 _nc_optimize_enable &= ~OPTIMIZE_MVCUR; 1305 (void) puts("Optimization is now off."); 1306 } else { 1307 _nc_optimize_enable |= OPTIMIZE_MVCUR; 1308 (void) puts("Optimization is now on."); 1309 } 1310 } 1311 /* 1312 * You can use the `t' test to profile and tune the movement 1313 * optimizer. Use iteration values in three digits or more. 1314 * At above 5000 iterations the profile timing averages are stable 1315 * to within a millisecond or three. 1316 * 1317 * The `overhead' field of the report will help you pick a 1318 * COMPUTE_OVERHEAD figure appropriate for your processor and 1319 * expected line speed. The `total estimated time' is 1320 * computation time plus a character-transmission time 1321 * estimate computed from the number of transmits and the baud 1322 * rate. 1323 * 1324 * Use this together with the `o' command to get a read on the 1325 * optimizer's effectiveness. Compare the total estimated times 1326 * for `t' runs of the same length in both optimized and un-optimized 1327 * modes. As long as the optimized times are less, the optimizer 1328 * is winning. 1329 */ 1330 else if (sscanf(buf, "t %d", &n) == 1) { 1331 float cumtime = 0.0, perchar; 1332 int speeds[] = 1333 {2400, 9600, 14400, 19200, 28800, 38400, 0}; 1334 1335 srand((unsigned) (getpid() + time((time_t *) 0))); 1336 profiling = TRUE; 1337 xmits = 0; 1338 for (i = 0; i < n; i++) { 1339 /* 1340 * This does a move test between two random locations, 1341 * Random moves probably short-change the optimizer, 1342 * which will work better on the short moves probably 1343 * typical of doupdate()'s usage pattern. Still, 1344 * until we have better data... 1345 */ 1346 #ifdef FIND_COREDUMP 1347 int from_y = roll(lines); 1348 int to_y = roll(lines); 1349 int from_x = roll(columns); 1350 int to_x = roll(columns); 1351 1352 printf("(%d,%d) -> (%d,%d)\n", from_y, from_x, to_y, to_x); 1353 mvcur(from_y, from_x, to_y, to_x); 1354 #else 1355 mvcur(roll(lines), roll(columns), roll(lines), roll(columns)); 1356 #endif /* FIND_COREDUMP */ 1357 if (diff) 1358 cumtime += diff; 1359 } 1360 profiling = FALSE; 1361 1362 /* 1363 * Average milliseconds per character optimization time. 1364 * This is the key figure to watch when tuning the optimizer. 1365 */ 1366 perchar = cumtime / n; 1367 1368 (void) printf("%d moves (%ld chars) in %d msec, %f msec each:\n", 1369 n, xmits, (int) cumtime, perchar); 1370 1371 for (i = 0; speeds[i]; i++) { 1372 /* 1373 * Total estimated time for the moves, computation and 1374 * transmission both. Transmission time is an estimate 1375 * assuming 9 bits/char, 8 bits + 1 stop bit. 1376 */ 1377 float totalest = cumtime + xmits * 9 * 1e6 / speeds[i]; 1378 1379 /* 1380 * Per-character optimization overhead in character transmits 1381 * at the current speed. Round this to the nearest integer 1382 * to figure COMPUTE_OVERHEAD for the speed. 1383 */ 1384 float overhead = speeds[i] * perchar / 1e6; 1385 1386 (void) 1387 printf("%6d bps: %3.2f char-xmits overhead; total estimated time %15.2f\n", 1388 speeds[i], overhead, totalest); 1389 } 1390 } else if (buf[0] == 'c') { 1391 (void) printf("char padding: %d\n", CURRENT_SCREEN->_char_padding); 1392 (void) printf("cr cost: %d\n", CURRENT_SCREEN->_cr_cost); 1393 (void) printf("cup cost: %d\n", CURRENT_SCREEN->_cup_cost); 1394 (void) printf("home cost: %d\n", CURRENT_SCREEN->_home_cost); 1395 (void) printf("ll cost: %d\n", CURRENT_SCREEN->_ll_cost); 1396 #if USE_HARD_TABS 1397 (void) printf("ht cost: %d\n", CURRENT_SCREEN->_ht_cost); 1398 (void) printf("cbt cost: %d\n", CURRENT_SCREEN->_cbt_cost); 1399 #endif /* USE_HARD_TABS */ 1400 (void) printf("cub1 cost: %d\n", CURRENT_SCREEN->_cub1_cost); 1401 (void) printf("cuf1 cost: %d\n", CURRENT_SCREEN->_cuf1_cost); 1402 (void) printf("cud1 cost: %d\n", CURRENT_SCREEN->_cud1_cost); 1403 (void) printf("cuu1 cost: %d\n", CURRENT_SCREEN->_cuu1_cost); 1404 (void) printf("cub cost: %d\n", CURRENT_SCREEN->_cub_cost); 1405 (void) printf("cuf cost: %d\n", CURRENT_SCREEN->_cuf_cost); 1406 (void) printf("cud cost: %d\n", CURRENT_SCREEN->_cud_cost); 1407 (void) printf("cuu cost: %d\n", CURRENT_SCREEN->_cuu_cost); 1408 (void) printf("hpa cost: %d\n", CURRENT_SCREEN->_hpa_cost); 1409 (void) printf("vpa cost: %d\n", CURRENT_SCREEN->_vpa_cost); 1410 } else if (buf[0] == 'x' || buf[0] == 'q') 1411 break; 1412 else 1413 (void) puts("Invalid command."); 1414 } 1415 1416 (void) fputs("rmcup:", stdout); 1417 _nc_mvcur_wrap(); 1418 putchar('\n'); 1419 1420 return (0); 1421 } 1422 1423 #endif /* MAIN */ 1424 1425 /* lib_mvcur.c ends here */ 1426