1 /**************************************************************************** 2 * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 * and: Thomas E. Dickey 1996 on * 33 ****************************************************************************/ 34 35 #define __INTERNAL_CAPS_VISIBLE 36 #include <progs.priv.h> 37 38 #include "dump_entry.h" 39 #include "termsort.c" /* this C file is generated */ 40 #include <parametrized.h> /* so is this */ 41 42 MODULE_ID("$Id: dump_entry.c,v 1.81 2007/08/25 20:05:35 tom Exp $") 43 44 #define INDENT 8 45 #define DISCARD(string) string = ABSENT_STRING 46 #define PRINTF (void) printf 47 48 typedef struct { 49 char *text; 50 size_t used; 51 size_t size; 52 } DYNBUF; 53 54 static int tversion; /* terminfo version */ 55 static int outform; /* output format to use */ 56 static int sortmode; /* sort mode to use */ 57 static int width = 60; /* max line width for listings */ 58 static int column; /* current column, limited by 'width' */ 59 static int oldcol; /* last value of column before wrap */ 60 static bool pretty; /* true if we format if-then-else strings */ 61 62 static char *save_sgr; 63 64 static DYNBUF outbuf; 65 static DYNBUF tmpbuf; 66 67 /* indirection pointers for implementing sort and display modes */ 68 static const PredIdx *bool_indirect, *num_indirect, *str_indirect; 69 static NCURSES_CONST char *const *bool_names; 70 static NCURSES_CONST char *const *num_names; 71 static NCURSES_CONST char *const *str_names; 72 73 static const char *separator, *trailer; 74 75 /* cover various ports and variants of terminfo */ 76 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */ 77 #define V_SVR1 1 /* SVR1, Ultrix */ 78 #define V_HPUX 2 /* HP/UX */ 79 #define V_AIX 3 /* AIX */ 80 #define V_BSD 4 /* BSD */ 81 82 #if NCURSES_XNAMES 83 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T')) 84 #else 85 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T') 86 #endif 87 88 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n)) 89 90 #if NCURSES_XNAMES 91 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j])) 92 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j])) 93 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j])) 94 #else 95 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j]) 96 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j]) 97 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j]) 98 #endif 99 100 static void 101 strncpy_DYN(DYNBUF * dst, const char *src, size_t need) 102 { 103 size_t want = need + dst->used + 1; 104 if (want > dst->size) { 105 dst->size += (want + 1024); /* be generous */ 106 dst->text = typeRealloc(char, dst->size, dst->text); 107 } 108 (void) strncpy(dst->text + dst->used, src, need); 109 dst->used += need; 110 dst->text[dst->used] = 0; 111 } 112 113 static void 114 strcpy_DYN(DYNBUF * dst, const char *src) 115 { 116 if (src == 0) { 117 dst->used = 0; 118 strcpy_DYN(dst, ""); 119 } else { 120 strncpy_DYN(dst, src, strlen(src)); 121 } 122 } 123 124 #if NO_LEAKS 125 static void 126 free_DYN(DYNBUF * p) 127 { 128 if (p->text != 0) 129 free(p->text); 130 p->text = 0; 131 p->size = 0; 132 p->used = 0; 133 } 134 135 void 136 _nc_leaks_dump_entry(void) 137 { 138 free_DYN(&outbuf); 139 free_DYN(&tmpbuf); 140 } 141 #endif 142 143 NCURSES_CONST char * 144 nametrans(const char *name) 145 /* translate a capability name from termcap to terminfo */ 146 { 147 const struct name_table_entry *np; 148 149 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) 150 switch (np->nte_type) { 151 case BOOLEAN: 152 if (bool_from_termcap[np->nte_index]) 153 return (boolcodes[np->nte_index]); 154 break; 155 156 case NUMBER: 157 if (num_from_termcap[np->nte_index]) 158 return (numcodes[np->nte_index]); 159 break; 160 161 case STRING: 162 if (str_from_termcap[np->nte_index]) 163 return (strcodes[np->nte_index]); 164 break; 165 } 166 167 return (0); 168 } 169 170 void 171 dump_init(const char *version, int mode, int sort, int twidth, int traceval, 172 bool formatted) 173 /* set up for entry display */ 174 { 175 width = twidth; 176 pretty = formatted; 177 178 /* versions */ 179 if (version == 0) 180 tversion = V_ALLCAPS; 181 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1") 182 || !strcmp(version, "Ultrix")) 183 tversion = V_SVR1; 184 else if (!strcmp(version, "HP")) 185 tversion = V_HPUX; 186 else if (!strcmp(version, "AIX")) 187 tversion = V_AIX; 188 else if (!strcmp(version, "BSD")) 189 tversion = V_BSD; 190 else 191 tversion = V_ALLCAPS; 192 193 /* implement display modes */ 194 switch (outform = mode) { 195 case F_LITERAL: 196 case F_TERMINFO: 197 bool_names = boolnames; 198 num_names = numnames; 199 str_names = strnames; 200 separator = twidth ? ", " : ","; 201 trailer = "\n\t"; 202 break; 203 204 case F_VARIABLE: 205 bool_names = boolfnames; 206 num_names = numfnames; 207 str_names = strfnames; 208 separator = twidth ? ", " : ","; 209 trailer = "\n\t"; 210 break; 211 212 case F_TERMCAP: 213 case F_TCONVERR: 214 bool_names = boolcodes; 215 num_names = numcodes; 216 str_names = strcodes; 217 separator = ":"; 218 trailer = "\\\n\t:"; 219 break; 220 } 221 222 /* implement sort modes */ 223 switch (sortmode = sort) { 224 case S_NOSORT: 225 if (traceval) 226 (void) fprintf(stderr, 227 "%s: sorting by term structure order\n", _nc_progname); 228 break; 229 230 case S_TERMINFO: 231 if (traceval) 232 (void) fprintf(stderr, 233 "%s: sorting by terminfo name order\n", _nc_progname); 234 bool_indirect = bool_terminfo_sort; 235 num_indirect = num_terminfo_sort; 236 str_indirect = str_terminfo_sort; 237 break; 238 239 case S_VARIABLE: 240 if (traceval) 241 (void) fprintf(stderr, 242 "%s: sorting by C variable order\n", _nc_progname); 243 bool_indirect = bool_variable_sort; 244 num_indirect = num_variable_sort; 245 str_indirect = str_variable_sort; 246 break; 247 248 case S_TERMCAP: 249 if (traceval) 250 (void) fprintf(stderr, 251 "%s: sorting by termcap name order\n", _nc_progname); 252 bool_indirect = bool_termcap_sort; 253 num_indirect = num_termcap_sort; 254 str_indirect = str_termcap_sort; 255 break; 256 } 257 258 if (traceval) 259 (void) fprintf(stderr, 260 "%s: width = %d, tversion = %d, outform = %d\n", 261 _nc_progname, width, tversion, outform); 262 } 263 264 static TERMTYPE *cur_type; 265 266 static int 267 dump_predicate(PredType type, PredIdx idx) 268 /* predicate function to use for ordinary decompilation */ 269 { 270 switch (type) { 271 case BOOLEAN: 272 return (cur_type->Booleans[idx] == FALSE) 273 ? FAIL : cur_type->Booleans[idx]; 274 275 case NUMBER: 276 return (cur_type->Numbers[idx] == ABSENT_NUMERIC) 277 ? FAIL : cur_type->Numbers[idx]; 278 279 case STRING: 280 return (cur_type->Strings[idx] != ABSENT_STRING) 281 ? (int) TRUE : FAIL; 282 } 283 284 return (FALSE); /* pacify compiler */ 285 } 286 287 static void set_obsolete_termcaps(TERMTYPE *tp); 288 289 /* is this the index of a function key string? */ 290 #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268)) 291 292 /* 293 * If we configure with a different Caps file, the offsets into the arrays 294 * will change. So we use an address expression. 295 */ 296 #define BOOL_IDX(name) (&(name) - &(CUR Booleans[0])) 297 #define NUM_IDX(name) (&(name) - &(CUR Numbers[0])) 298 #define STR_IDX(name) (&(name) - &(CUR Strings[0])) 299 300 static bool 301 version_filter(PredType type, PredIdx idx) 302 /* filter out capabilities we may want to suppress */ 303 { 304 switch (tversion) { 305 case V_ALLCAPS: /* SVr4, XSI Curses */ 306 return (TRUE); 307 308 case V_SVR1: /* System V Release 1, Ultrix */ 309 switch (type) { 310 case BOOLEAN: 311 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE); 312 case NUMBER: 313 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE); 314 case STRING: 315 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE); 316 } 317 break; 318 319 case V_HPUX: /* Hewlett-Packard */ 320 switch (type) { 321 case BOOLEAN: 322 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE); 323 case NUMBER: 324 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE); 325 case STRING: 326 if (idx <= STR_IDX(prtr_non)) 327 return (TRUE); 328 else if (FNKEY(idx)) /* function keys */ 329 return (TRUE); 330 else if (idx == STR_IDX(plab_norm) 331 || idx == STR_IDX(label_on) 332 || idx == STR_IDX(label_off)) 333 return (TRUE); 334 else 335 return (FALSE); 336 } 337 break; 338 339 case V_AIX: /* AIX */ 340 switch (type) { 341 case BOOLEAN: 342 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE); 343 case NUMBER: 344 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE); 345 case STRING: 346 if (idx <= STR_IDX(prtr_non)) 347 return (TRUE); 348 else if (FNKEY(idx)) /* function keys */ 349 return (TRUE); 350 else 351 return (FALSE); 352 } 353 break; 354 355 #define is_termcap(type) (idx < (int) sizeof(type##_from_termcap) && \ 356 type##_from_termcap[idx]) 357 358 case V_BSD: /* BSD */ 359 switch (type) { 360 case BOOLEAN: 361 return is_termcap(bool); 362 case NUMBER: 363 return is_termcap(num); 364 case STRING: 365 return is_termcap(str); 366 } 367 break; 368 } 369 370 return (FALSE); /* pacify the compiler */ 371 } 372 373 static void 374 trim_trailing(void) 375 { 376 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ') 377 outbuf.text[--outbuf.used] = '\0'; 378 } 379 380 static void 381 force_wrap(void) 382 { 383 oldcol = column; 384 trim_trailing(); 385 strcpy_DYN(&outbuf, trailer); 386 column = INDENT; 387 } 388 389 static void 390 wrap_concat(const char *src) 391 { 392 int need = strlen(src); 393 int want = strlen(separator) + need; 394 395 if (column > INDENT 396 && column + want > width) { 397 force_wrap(); 398 } 399 strcpy_DYN(&outbuf, src); 400 strcpy_DYN(&outbuf, separator); 401 column += need; 402 } 403 404 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \ 405 if ((size_t)(last - first) > sizeof(sep_trail)-1 \ 406 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \ 407 first += sizeof(sep_trail)-2 408 409 /* Returns the nominal length of the buffer assuming it is termcap format, 410 * i.e., the continuation sequence is treated as a single character ":". 411 * 412 * There are several implementations of termcap which read the text into a 413 * fixed-size buffer. Generally they strip the newlines from the text, but may 414 * not do it until after the buffer is read. Also, "tc=" resolution may be 415 * expanded in the same buffer. This function is useful for measuring the size 416 * of the best fixed-buffer implementation; the worst case may be much worse. 417 */ 418 #ifdef TEST_TERMCAP_LENGTH 419 static int 420 termcap_length(const char *src) 421 { 422 static const char pattern[] = ":\\\n\t:"; 423 424 int len = 0; 425 const char *const t = src + strlen(src); 426 427 while (*src != '\0') { 428 IGNORE_SEP_TRAIL(src, t, pattern); 429 src++; 430 len++; 431 } 432 return len; 433 } 434 #else 435 #define termcap_length(src) strlen(src) 436 #endif 437 438 static void 439 indent_DYN(DYNBUF * buffer, int level) 440 { 441 int n; 442 443 for (n = 0; n < level; n++) 444 strncpy_DYN(buffer, "\t", 1); 445 } 446 447 static bool 448 has_params(const char *src) 449 { 450 bool result = FALSE; 451 int len = strlen(src); 452 int n; 453 bool ifthen = FALSE; 454 bool params = FALSE; 455 456 for (n = 0; n < len - 1; ++n) { 457 if (!strncmp(src + n, "%p", 2)) { 458 params = TRUE; 459 } else if (!strncmp(src + n, "%;", 2)) { 460 ifthen = TRUE; 461 result = params; 462 break; 463 } 464 } 465 if (!ifthen) { 466 result = ((len > 50) && params); 467 } 468 return result; 469 } 470 471 static char * 472 fmt_complex(char *src, int level) 473 { 474 bool percent = FALSE; 475 bool params = has_params(src); 476 477 while (*src != '\0') { 478 switch (*src) { 479 case '\\': 480 percent = FALSE; 481 strncpy_DYN(&tmpbuf, src++, 1); 482 break; 483 case '%': 484 percent = TRUE; 485 break; 486 case '?': /* "if" */ 487 case 't': /* "then" */ 488 case 'e': /* "else" */ 489 if (percent) { 490 percent = FALSE; 491 tmpbuf.text[tmpbuf.used - 1] = '\n'; 492 /* treat a "%e" as else-if, on the same level */ 493 if (*src == 'e') { 494 indent_DYN(&tmpbuf, level); 495 strncpy_DYN(&tmpbuf, "%", 1); 496 strncpy_DYN(&tmpbuf, src, 1); 497 src++; 498 params = has_params(src); 499 if (!params && *src != '\0' && *src != '%') { 500 strncpy_DYN(&tmpbuf, "\n", 1); 501 indent_DYN(&tmpbuf, level + 1); 502 } 503 } else { 504 indent_DYN(&tmpbuf, level + 1); 505 strncpy_DYN(&tmpbuf, "%", 1); 506 strncpy_DYN(&tmpbuf, src, 1); 507 if (*src++ == '?') { 508 src = fmt_complex(src, level + 1); 509 if (*src != '\0' && *src != '%') { 510 strncpy_DYN(&tmpbuf, "\n", 1); 511 indent_DYN(&tmpbuf, level + 1); 512 } 513 } else if (level == 1) { 514 _nc_warning("%%%c without %%?", *src); 515 } 516 } 517 continue; 518 } 519 break; 520 case ';': /* "endif" */ 521 if (percent) { 522 percent = FALSE; 523 if (level > 1) { 524 tmpbuf.text[tmpbuf.used - 1] = '\n'; 525 indent_DYN(&tmpbuf, level); 526 strncpy_DYN(&tmpbuf, "%", 1); 527 strncpy_DYN(&tmpbuf, src++, 1); 528 return src; 529 } 530 _nc_warning("%%; without %%?"); 531 } 532 break; 533 case 'p': 534 if (percent && params) { 535 tmpbuf.text[tmpbuf.used - 1] = '\n'; 536 indent_DYN(&tmpbuf, level + 1); 537 strncpy_DYN(&tmpbuf, "%", 1); 538 } 539 params = FALSE; 540 percent = FALSE; 541 break; 542 default: 543 percent = FALSE; 544 break; 545 } 546 strncpy_DYN(&tmpbuf, src++, 1); 547 } 548 return src; 549 } 550 551 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap) 552 553 int 554 fmt_entry(TERMTYPE *tterm, 555 PredFunc pred, 556 bool content_only, 557 bool suppress_untranslatable, 558 bool infodump, 559 int numbers) 560 { 561 PredIdx i, j; 562 char buffer[MAX_TERMINFO_LENGTH]; 563 char *capability; 564 NCURSES_CONST char *name; 565 int predval, len; 566 PredIdx num_bools = 0; 567 PredIdx num_values = 0; 568 PredIdx num_strings = 0; 569 bool outcount = 0; 570 571 #define WRAP_CONCAT \ 572 wrap_concat(buffer); \ 573 outcount = TRUE 574 575 len = 12; /* terminfo file-header */ 576 577 if (pred == 0) { 578 cur_type = tterm; 579 pred = dump_predicate; 580 } 581 582 strcpy_DYN(&outbuf, 0); 583 if (content_only) { 584 column = INDENT; /* FIXME: workaround to prevent empty lines */ 585 } else { 586 strcpy_DYN(&outbuf, tterm->term_names); 587 strcpy_DYN(&outbuf, separator); 588 column = outbuf.used; 589 force_wrap(); 590 } 591 592 for_each_boolean(j, tterm) { 593 i = BoolIndirect(j); 594 name = ExtBoolname(tterm, i, bool_names); 595 596 if (!version_filter(BOOLEAN, i)) 597 continue; 598 else if (isObsolete(outform, name)) 599 continue; 600 601 predval = pred(BOOLEAN, i); 602 if (predval != FAIL) { 603 (void) strcpy(buffer, name); 604 if (predval <= 0) 605 (void) strcat(buffer, "@"); 606 else if (i + 1 > num_bools) 607 num_bools = i + 1; 608 WRAP_CONCAT; 609 } 610 } 611 612 if (column != INDENT) 613 force_wrap(); 614 615 for_each_number(j, tterm) { 616 i = NumIndirect(j); 617 name = ExtNumname(tterm, i, num_names); 618 619 if (!version_filter(NUMBER, i)) 620 continue; 621 else if (isObsolete(outform, name)) 622 continue; 623 624 predval = pred(NUMBER, i); 625 if (predval != FAIL) { 626 if (tterm->Numbers[i] < 0) { 627 sprintf(buffer, "%s@", name); 628 } else { 629 sprintf(buffer, "%s#%d", name, tterm->Numbers[i]); 630 if (i + 1 > num_values) 631 num_values = i + 1; 632 } 633 WRAP_CONCAT; 634 } 635 } 636 637 if (column != INDENT) 638 force_wrap(); 639 640 len += num_bools 641 + num_values * 2 642 + strlen(tterm->term_names) + 1; 643 if (len & 1) 644 len++; 645 646 #undef CUR 647 #define CUR tterm-> 648 if (outform == F_TERMCAP) { 649 if (termcap_reset != ABSENT_STRING) { 650 if (init_3string != ABSENT_STRING 651 && !strcmp(init_3string, termcap_reset)) 652 DISCARD(init_3string); 653 654 if (reset_2string != ABSENT_STRING 655 && !strcmp(reset_2string, termcap_reset)) 656 DISCARD(reset_2string); 657 } 658 } 659 660 for_each_string(j, tterm) { 661 i = StrIndirect(j); 662 name = ExtStrname(tterm, i, str_names); 663 capability = tterm->Strings[i]; 664 665 if (!version_filter(STRING, i)) 666 continue; 667 else if (isObsolete(outform, name)) 668 continue; 669 670 #if NCURSES_XNAMES 671 /* 672 * Extended names can be longer than 2 characters, but termcap programs 673 * cannot read those (filter them out). 674 */ 675 if (outform == F_TERMCAP && (strlen(name) > 2)) 676 continue; 677 #endif 678 679 if (outform == F_TERMCAP) { 680 /* 681 * Some older versions of vi want rmir/smir to be defined 682 * for ich/ich1 to work. If they're not defined, force 683 * them to be output as defined and empty. 684 */ 685 if (PRESENT(insert_character) || PRESENT(parm_ich)) { 686 if (SAME_CAP(i, enter_insert_mode) 687 && enter_insert_mode == ABSENT_STRING) { 688 (void) strcpy(buffer, "im="); 689 WRAP_CONCAT; 690 continue; 691 } 692 693 if (SAME_CAP(i, exit_insert_mode) 694 && exit_insert_mode == ABSENT_STRING) { 695 (void) strcpy(buffer, "ei="); 696 WRAP_CONCAT; 697 continue; 698 } 699 } 700 /* 701 * termcap applications such as screen will be confused if sgr0 702 * is translated to a string containing rmacs. Filter that out. 703 */ 704 if (PRESENT(exit_attribute_mode)) { 705 if (SAME_CAP(i, exit_attribute_mode)) { 706 char *trimmed_sgr0; 707 char *my_sgr = set_attributes; 708 709 set_attributes = save_sgr; 710 711 trimmed_sgr0 = _nc_trim_sgr0(tterm); 712 if (strcmp(capability, trimmed_sgr0)) 713 capability = trimmed_sgr0; 714 715 set_attributes = my_sgr; 716 } 717 } 718 } 719 720 predval = pred(STRING, i); 721 buffer[0] = '\0'; 722 723 if (predval != FAIL) { 724 if (capability != ABSENT_STRING 725 && i + 1 > num_strings) 726 num_strings = i + 1; 727 728 if (!VALID_STRING(capability)) { 729 sprintf(buffer, "%s@", name); 730 WRAP_CONCAT; 731 } else if (outform == F_TERMCAP || outform == F_TCONVERR) { 732 int params = ((i < (int) SIZEOF(parametrized)) 733 ? parametrized[i] 734 : 0); 735 char *srccap = _nc_tic_expand(capability, TRUE, numbers); 736 char *cv = _nc_infotocap(name, srccap, params); 737 738 if (cv == 0) { 739 if (outform == F_TCONVERR) { 740 sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!", 741 name, srccap); 742 } else if (suppress_untranslatable) { 743 continue; 744 } else { 745 char *s = srccap, *d = buffer; 746 sprintf(d, "..%s=", name); 747 d += strlen(d); 748 while ((*d = *s++) != 0) { 749 if (*d == ':') { 750 *d++ = '\\'; 751 *d = ':'; 752 } else if (*d == '\\') { 753 *++d = *s++; 754 } 755 d++; 756 } 757 } 758 } else { 759 sprintf(buffer, "%s=%s", name, cv); 760 } 761 len += strlen(capability) + 1; 762 WRAP_CONCAT; 763 } else { 764 char *src = _nc_tic_expand(capability, 765 outform == F_TERMINFO, numbers); 766 767 strcpy_DYN(&tmpbuf, 0); 768 strcpy_DYN(&tmpbuf, name); 769 strcpy_DYN(&tmpbuf, "="); 770 if (pretty 771 && (outform == F_TERMINFO 772 || outform == F_VARIABLE)) { 773 fmt_complex(src, 1); 774 } else { 775 strcpy_DYN(&tmpbuf, src); 776 } 777 len += strlen(capability) + 1; 778 wrap_concat(tmpbuf.text); 779 outcount = TRUE; 780 } 781 } 782 /* e.g., trimmed_sgr0 */ 783 if (capability != tterm->Strings[i]) 784 free(capability); 785 } 786 len += num_strings * 2; 787 788 /* 789 * This piece of code should be an effective inverse of the functions 790 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c. 791 * Much more work should be done on this to support dumping termcaps. 792 */ 793 if (tversion == V_HPUX) { 794 if (VALID_STRING(memory_lock)) { 795 (void) sprintf(buffer, "meml=%s", memory_lock); 796 WRAP_CONCAT; 797 } 798 if (VALID_STRING(memory_unlock)) { 799 (void) sprintf(buffer, "memu=%s", memory_unlock); 800 WRAP_CONCAT; 801 } 802 } else if (tversion == V_AIX) { 803 if (VALID_STRING(acs_chars)) { 804 bool box_ok = TRUE; 805 const char *acstrans = "lqkxjmwuvtn"; 806 const char *cp; 807 char *tp, *sp, boxchars[11]; 808 809 tp = boxchars; 810 for (cp = acstrans; *cp; cp++) { 811 sp = strchr(acs_chars, *cp); 812 if (sp) 813 *tp++ = sp[1]; 814 else { 815 box_ok = FALSE; 816 break; 817 } 818 } 819 tp[0] = '\0'; 820 821 if (box_ok) { 822 (void) strcpy(buffer, "box1="); 823 (void) strcat(buffer, _nc_tic_expand(boxchars, 824 outform == F_TERMINFO, numbers)); 825 WRAP_CONCAT; 826 } 827 } 828 } 829 830 /* 831 * kludge: trim off trailer to avoid an extra blank line 832 * in infocmp -u output when there are no string differences 833 */ 834 if (outcount) { 835 bool trimmed = FALSE; 836 j = outbuf.used; 837 if (j >= 2 838 && outbuf.text[j - 1] == '\t' 839 && outbuf.text[j - 2] == '\n') { 840 outbuf.used -= 2; 841 trimmed = TRUE; 842 } else if (j >= 4 843 && outbuf.text[j - 1] == ':' 844 && outbuf.text[j - 2] == '\t' 845 && outbuf.text[j - 3] == '\n' 846 && outbuf.text[j - 4] == '\\') { 847 outbuf.used -= 4; 848 trimmed = TRUE; 849 } 850 if (trimmed) { 851 outbuf.text[outbuf.used] = '\0'; 852 column = oldcol; 853 strcpy_DYN(&outbuf, " "); 854 } 855 } 856 #if 0 857 fprintf(stderr, "num_bools = %d\n", num_bools); 858 fprintf(stderr, "num_values = %d\n", num_values); 859 fprintf(stderr, "num_strings = %d\n", num_strings); 860 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n", 861 tterm->term_names, len, outbuf.used, outbuf.text); 862 #endif 863 /* 864 * Here's where we use infodump to trigger a more stringent length check 865 * for termcap-translation purposes. 866 * Return the length of the raw entry, without tc= expansions, 867 * It gives an idea of which entries are deadly to even *scan past*, 868 * as opposed to *use*. 869 */ 870 return (infodump ? len : (int) termcap_length(outbuf.text)); 871 } 872 873 static bool 874 kill_string(TERMTYPE *tterm, char *cap) 875 { 876 int n; 877 for (n = 0; n < NUM_STRINGS(tterm); ++n) { 878 if (cap == tterm->Strings[n]) { 879 tterm->Strings[n] = ABSENT_STRING; 880 return TRUE; 881 } 882 } 883 return FALSE; 884 } 885 886 static char * 887 find_string(TERMTYPE *tterm, char *name) 888 { 889 PredIdx n; 890 for (n = 0; n < NUM_STRINGS(tterm); ++n) { 891 if (version_filter(STRING, n) 892 && !strcmp(name, strnames[n])) { 893 char *cap = tterm->Strings[n]; 894 if (VALID_STRING(cap)) { 895 return cap; 896 } 897 break; 898 } 899 } 900 return ABSENT_STRING; 901 } 902 903 /* 904 * This is used to remove function-key labels from a termcap entry to 905 * make it smaller. 906 */ 907 static int 908 kill_labels(TERMTYPE *tterm, int target) 909 { 910 int n; 911 int result = 0; 912 char *cap; 913 char name[10]; 914 915 for (n = 0; n <= 10; ++n) { 916 sprintf(name, "lf%d", n); 917 if ((cap = find_string(tterm, name)) != ABSENT_STRING 918 && kill_string(tterm, cap)) { 919 target -= (strlen(cap) + 5); 920 ++result; 921 if (target < 0) 922 break; 923 } 924 } 925 return result; 926 } 927 928 /* 929 * This is used to remove function-key definitions from a termcap entry to 930 * make it smaller. 931 */ 932 static int 933 kill_fkeys(TERMTYPE *tterm, int target) 934 { 935 int n; 936 int result = 0; 937 char *cap; 938 char name[10]; 939 940 for (n = 60; n >= 0; --n) { 941 sprintf(name, "kf%d", n); 942 if ((cap = find_string(tterm, name)) != ABSENT_STRING 943 && kill_string(tterm, cap)) { 944 target -= (strlen(cap) + 5); 945 ++result; 946 if (target < 0) 947 break; 948 } 949 } 950 return result; 951 } 952 953 /* 954 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100. 955 * Also, since this is for termcap, we only care about the line-drawing map. 956 */ 957 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0) 958 959 static bool 960 one_one_mapping(const char *mapping) 961 { 962 bool result = TRUE; 963 964 if (mapping != ABSENT_STRING) { 965 int n = 0; 966 while (mapping[n] != '\0') { 967 if (isLine(mapping[n]) && 968 mapping[n] != mapping[n + 1]) { 969 result = FALSE; 970 break; 971 } 972 n += 2; 973 } 974 } 975 return result; 976 } 977 978 #define FMT_ENTRY() \ 979 fmt_entry(tterm, pred, \ 980 0, \ 981 suppress_untranslatable, \ 982 infodump, numbers) 983 984 #define SHOW_WHY PRINTF 985 986 static bool 987 purged_acs(TERMTYPE *tterm) 988 { 989 bool result = FALSE; 990 991 if (VALID_STRING(acs_chars)) { 992 if (!one_one_mapping(acs_chars)) { 993 enter_alt_charset_mode = ABSENT_STRING; 994 exit_alt_charset_mode = ABSENT_STRING; 995 SHOW_WHY("# (rmacs/smacs removed for consistency)\n"); 996 } 997 result = TRUE; 998 } 999 return result; 1000 } 1001 1002 /* 1003 * Dump a single entry. 1004 */ 1005 void 1006 dump_entry(TERMTYPE *tterm, 1007 bool suppress_untranslatable, 1008 bool limited, 1009 int numbers, 1010 PredFunc pred) 1011 { 1012 TERMTYPE save_tterm; 1013 int len, critlen; 1014 const char *legend; 1015 bool infodump; 1016 1017 if (outform == F_TERMCAP || outform == F_TCONVERR) { 1018 critlen = MAX_TERMCAP_LENGTH; 1019 legend = "older termcap"; 1020 infodump = FALSE; 1021 set_obsolete_termcaps(tterm); 1022 } else { 1023 critlen = MAX_TERMINFO_LENGTH; 1024 legend = "terminfo"; 1025 infodump = TRUE; 1026 } 1027 1028 save_sgr = set_attributes; 1029 1030 if (((len = FMT_ENTRY()) > critlen) 1031 && limited) { 1032 1033 save_tterm = *tterm; 1034 if (!suppress_untranslatable) { 1035 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n", 1036 critlen); 1037 suppress_untranslatable = TRUE; 1038 } 1039 if ((len = FMT_ENTRY()) > critlen) { 1040 /* 1041 * We pick on sgr because it's a nice long string capability that 1042 * is really just an optimization hack. Another good candidate is 1043 * acsc since it is both long and unused by BSD termcap. 1044 */ 1045 bool changed = FALSE; 1046 1047 #if NCURSES_XNAMES 1048 /* 1049 * Extended names are most likely function-key definitions. Drop 1050 * those first. 1051 */ 1052 int n; 1053 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) { 1054 const char *name = ExtStrname(tterm, n, strnames); 1055 1056 if (VALID_STRING(tterm->Strings[n])) { 1057 set_attributes = ABSENT_STRING; 1058 /* we remove long names anyway - only report the short */ 1059 if (strlen(name) <= 2) { 1060 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n", 1061 name, 1062 critlen); 1063 } 1064 changed = TRUE; 1065 if ((len = FMT_ENTRY()) <= critlen) 1066 break; 1067 } 1068 } 1069 #endif 1070 if (VALID_STRING(set_attributes)) { 1071 set_attributes = ABSENT_STRING; 1072 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n", 1073 critlen); 1074 changed = TRUE; 1075 } 1076 if (!changed || ((len = FMT_ENTRY()) > critlen)) { 1077 if (purged_acs(tterm)) { 1078 acs_chars = ABSENT_STRING; 1079 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n", 1080 critlen); 1081 changed = TRUE; 1082 } 1083 } 1084 if (!changed || ((len = FMT_ENTRY()) > critlen)) { 1085 int oldversion = tversion; 1086 1087 tversion = V_BSD; 1088 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n", 1089 critlen); 1090 1091 len = FMT_ENTRY(); 1092 if (len > critlen 1093 && kill_labels(tterm, len - critlen)) { 1094 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n", 1095 critlen); 1096 len = FMT_ENTRY(); 1097 } 1098 if (len > critlen 1099 && kill_fkeys(tterm, len - critlen)) { 1100 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n", 1101 critlen); 1102 len = FMT_ENTRY(); 1103 } 1104 if (len > critlen) { 1105 (void) fprintf(stderr, 1106 "warning: %s entry is %d bytes long\n", 1107 _nc_first_name(tterm->term_names), 1108 len); 1109 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n", 1110 len, legend); 1111 } 1112 tversion = oldversion; 1113 } 1114 set_attributes = save_sgr; 1115 *tterm = save_tterm; 1116 } 1117 } else if (!version_filter(STRING, STR_IDX(acs_chars))) { 1118 save_tterm = *tterm; 1119 if (purged_acs(tterm)) { 1120 len = FMT_ENTRY(); 1121 } 1122 *tterm = save_tterm; 1123 } 1124 } 1125 1126 void 1127 dump_uses(const char *name, bool infodump) 1128 /* dump "use=" clauses in the appropriate format */ 1129 { 1130 char buffer[MAX_TERMINFO_LENGTH]; 1131 1132 if (outform == F_TERMCAP || outform == F_TCONVERR) 1133 trim_trailing(); 1134 (void) sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name); 1135 wrap_concat(buffer); 1136 } 1137 1138 int 1139 show_entry(void) 1140 { 1141 trim_trailing(); 1142 (void) fputs(outbuf.text, stdout); 1143 putchar('\n'); 1144 return outbuf.used; 1145 } 1146 1147 void 1148 compare_entry(void (*hook) (PredType t, PredIdx i, const char *name), 1149 TERMTYPE *tp GCC_UNUSED, 1150 bool quiet) 1151 /* compare two entries */ 1152 { 1153 PredIdx i, j; 1154 NCURSES_CONST char *name; 1155 1156 if (!quiet) 1157 fputs(" comparing booleans.\n", stdout); 1158 for_each_boolean(j, tp) { 1159 i = BoolIndirect(j); 1160 name = ExtBoolname(tp, i, bool_names); 1161 1162 if (isObsolete(outform, name)) 1163 continue; 1164 1165 (*hook) (CMP_BOOLEAN, i, name); 1166 } 1167 1168 if (!quiet) 1169 fputs(" comparing numbers.\n", stdout); 1170 for_each_number(j, tp) { 1171 i = NumIndirect(j); 1172 name = ExtNumname(tp, i, num_names); 1173 1174 if (isObsolete(outform, name)) 1175 continue; 1176 1177 (*hook) (CMP_NUMBER, i, name); 1178 } 1179 1180 if (!quiet) 1181 fputs(" comparing strings.\n", stdout); 1182 for_each_string(j, tp) { 1183 i = StrIndirect(j); 1184 name = ExtStrname(tp, i, str_names); 1185 1186 if (isObsolete(outform, name)) 1187 continue; 1188 1189 (*hook) (CMP_STRING, i, name); 1190 } 1191 1192 /* (void) fputs(" comparing use entries.\n", stdout); */ 1193 (*hook) (CMP_USE, 0, "use"); 1194 1195 } 1196 1197 #define NOTSET(s) ((s) == 0) 1198 1199 /* 1200 * This bit of legerdemain turns all the terminfo variable names into 1201 * references to locations in the arrays Booleans, Numbers, and Strings --- 1202 * precisely what's needed. 1203 */ 1204 #undef CUR 1205 #define CUR tp-> 1206 1207 static void 1208 set_obsolete_termcaps(TERMTYPE *tp) 1209 { 1210 #include "capdefaults.c" 1211 } 1212 1213 /* 1214 * Convert an alternate-character-set string to canonical form: sorted and 1215 * unique. 1216 */ 1217 void 1218 repair_acsc(TERMTYPE *tp) 1219 { 1220 if (VALID_STRING(acs_chars)) { 1221 size_t n, m; 1222 char mapped[256]; 1223 char extra = 0; 1224 unsigned source; 1225 unsigned target; 1226 bool fix_needed = FALSE; 1227 1228 for (n = 0, source = 0; acs_chars[n] != 0; n++) { 1229 target = UChar(acs_chars[n]); 1230 if (source >= target) { 1231 fix_needed = TRUE; 1232 break; 1233 } 1234 source = target; 1235 if (acs_chars[n + 1]) 1236 n++; 1237 } 1238 if (fix_needed) { 1239 memset(mapped, 0, sizeof(mapped)); 1240 for (n = 0; acs_chars[n] != 0; n++) { 1241 source = UChar(acs_chars[n]); 1242 if ((target = (unsigned char) acs_chars[n + 1]) != 0) { 1243 mapped[source] = target; 1244 n++; 1245 } else { 1246 extra = source; 1247 } 1248 } 1249 for (n = m = 0; n < sizeof(mapped); n++) { 1250 if (mapped[n]) { 1251 acs_chars[m++] = n; 1252 acs_chars[m++] = mapped[n]; 1253 } 1254 } 1255 if (extra) 1256 acs_chars[m++] = extra; /* garbage in, garbage out */ 1257 acs_chars[m] = 0; 1258 } 1259 } 1260 } 1261