1 /**************************************************************************** 2 * Copyright 2018-2023,2024 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 ****************************************************************************/ 35 36 /* 37 * write_entry.c -- write a terminfo structure onto the file system 38 */ 39 40 #include <curses.priv.h> 41 #include <hashed_db.h> 42 43 #include <tic.h> 44 45 MODULE_ID("$Id: write_entry.c,v 1.132 2024/04/20 17:58:51 tom Exp $") 46 47 #if 1 48 #define TRACE_OUT(p) DEBUG(2, p) 49 #define TRACE_NUM(n) if (VALID_NUMERIC(Numbers[n])) { \ 50 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) (n), Numbers[n])); } 51 #else 52 #define TRACE_OUT(p) /*nothing */ 53 #define TRACE_NUM(n) /* nothing */ 54 #endif 55 56 /* 57 * FIXME: special case to work around Cygwin bug in link(), which updates 58 * the target file's timestamp. 59 */ 60 #if HAVE_LINK && !USE_SYMLINKS && !MIXEDCASE_FILENAMES && defined(__CYGWIN__) 61 #define LINK_TOUCHES 1 62 #else 63 #define LINK_TOUCHES 0 64 #endif 65 66 static int total_written; 67 static int total_parts; 68 static int total_size; 69 70 static int make_db_root(const char *); 71 72 #if !USE_HASHED_DB 73 static void 74 write_file(char *filename, TERMTYPE2 *tp) 75 { 76 char buffer[MAX_ENTRY_SIZE]; 77 unsigned limit = sizeof(buffer); 78 unsigned offset = 0; 79 80 if (_nc_write_object(tp, buffer, &offset, limit) == ERR) { 81 _nc_warning("entry is larger than %u bytes", limit); 82 } else { 83 FILE *fp = ((_nc_access(filename, W_OK) == 0) 84 ? safe_fopen(filename, BIN_W) 85 : 0); 86 size_t actual; 87 88 if (fp == 0) { 89 perror(filename); 90 _nc_syserr_abort("cannot open %s/%s", _nc_tic_dir(0), filename); 91 } 92 93 actual = fwrite(buffer, sizeof(char), (size_t) offset, fp); 94 if (actual != offset) { 95 int myerr = ferror(fp) ? errno : 0; 96 if (myerr) { 97 _nc_syserr_abort("error writing %s/%s: %s", 98 _nc_tic_dir(NULL), 99 filename, 100 strerror(myerr)); 101 } else { 102 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu", 103 _nc_tic_dir(NULL), 104 filename, 105 offset, 106 (unsigned long) actual); 107 } 108 } else { 109 fclose(fp); 110 DEBUG(1, ("Created %s", filename)); 111 } 112 } 113 } 114 115 /* 116 * Check for access rights to destination directories 117 * Create any directories which don't exist. 118 * 119 * Note: there's no reason to return the result of make_db_root(), since 120 * this function is called only in instances where that has to succeed. 121 */ 122 static void 123 check_writeable(int code) 124 { 125 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 126 static bool verified[sizeof(dirnames)]; 127 128 char dir[sizeof(LEAF_FMT)]; 129 char *s = 0; 130 131 if (code == 0 || (s = (strchr) (dirnames, code)) == 0) { 132 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code); 133 } else if (!verified[s - dirnames]) { 134 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code); 135 if (make_db_root(dir) < 0) { 136 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(NULL), dir); 137 } else { 138 verified[s - dirnames] = TRUE; 139 } 140 } 141 } 142 #endif /* !USE_HASHED_DB */ 143 144 static int 145 make_db_path(char *dst, const char *src, size_t limit) 146 { 147 int rc = -1; 148 const char *top = _nc_tic_dir(NULL); 149 150 if (src == top || _nc_is_abs_path(src)) { 151 if (strlen(src) + 1 <= limit) { 152 _nc_STRCPY(dst, src, limit); 153 rc = 0; 154 } 155 } else { 156 if ((strlen(top) + strlen(src) + 6) <= limit) { 157 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src); 158 rc = 0; 159 } 160 } 161 #if USE_HASHED_DB 162 if (rc == 0) { 163 static const char suffix[] = DBM_SUFFIX; 164 size_t have = strlen(dst); 165 size_t need = strlen(suffix); 166 if (have > need && strcmp(dst + (int) (have - need), suffix)) { 167 if (have + need <= limit) { 168 _nc_STRCAT(dst, suffix, limit); 169 } else { 170 rc = -1; 171 } 172 } else if (_nc_is_dir_path(dst)) { 173 rc = -1; 174 } 175 } 176 #endif 177 return rc; 178 } 179 180 /* 181 * Make a database-root if it doesn't exist. 182 */ 183 static int 184 make_db_root(const char *path) 185 { 186 int rc; 187 char fullpath[PATH_MAX]; 188 189 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) { 190 #if USE_HASHED_DB 191 DB *capdbp; 192 193 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) { 194 rc = -1; 195 } else if (_nc_db_close(capdbp) < 0) { 196 rc = -1; 197 } 198 #else 199 struct stat statbuf; 200 201 if ((rc = stat(path, &statbuf)) == -1) { 202 rc = mkdir(path 203 #ifndef _NC_WINDOWS 204 ,0777 205 #endif 206 ); 207 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) { 208 rc = -1; /* permission denied */ 209 } else if (!(S_ISDIR(statbuf.st_mode))) { 210 rc = -1; /* not a directory */ 211 } 212 #endif 213 } 214 return rc; 215 } 216 217 /* 218 * Set the write directory for compiled entries. 219 */ 220 NCURSES_EXPORT(void) 221 _nc_set_writedir(const char *dir) 222 { 223 const char *destination; 224 char actual[PATH_MAX]; 225 bool specific = (dir != NULL); 226 227 if (!specific && use_terminfo_vars()) 228 dir = getenv("TERMINFO"); 229 230 if (dir != NULL) 231 (void) _nc_tic_dir(dir); 232 233 destination = _nc_tic_dir(NULL); 234 if (make_db_root(destination) < 0) { 235 bool success = FALSE; 236 237 if (!specific) { 238 char *home = _nc_home_terminfo(); 239 240 if (home != NULL) { 241 destination = home; 242 if (make_db_root(destination) == 0) 243 success = TRUE; 244 } 245 } 246 if (!success) { 247 _nc_err_abort("%s: permission denied (errno %d)", 248 destination, errno); 249 } 250 } 251 252 /* 253 * Note: because of this code, this logic should be exercised 254 * *once only* per run. 255 */ 256 #if USE_HASHED_DB 257 make_db_path(actual, destination, sizeof(actual)); 258 #else 259 if (chdir(_nc_tic_dir(destination)) < 0 260 || getcwd(actual, sizeof(actual)) == NULL) 261 _nc_err_abort("%s: not a directory", destination); 262 #endif 263 _nc_keep_tic_dir(actual); 264 } 265 266 /* 267 * Save the compiled version of a description in the filesystem. 268 * 269 * make a copy of the name-list 270 * break it up into first-name and all-but-last-name 271 * creat(first-name) 272 * write object information to first-name 273 * close(first-name) 274 * for each name in all-but-last-name 275 * link to first-name 276 * 277 * Using 'time()' to obtain a reference for file timestamps is unreliable, 278 * e.g., with NFS, because the filesystem may have a different time 279 * reference. We check for pre-existence of links by latching the first 280 * timestamp from a file that we create. 281 * 282 * The _nc_warning() calls will report a correct line number only if 283 * _nc_curr_line is properly set before the write_entry() call. 284 */ 285 286 NCURSES_EXPORT(void) 287 _nc_write_entry(TERMTYPE2 *const tp) 288 { 289 #if USE_HASHED_DB 290 291 char buffer[MAX_ENTRY_SIZE + 1]; 292 unsigned limit = sizeof(buffer); 293 unsigned offset = 0; 294 295 #else /* !USE_HASHED_DB */ 296 297 struct stat statbuf; 298 char filename[PATH_MAX]; 299 char linkname[PATH_MAX]; 300 #if USE_SYMLINKS 301 char symlinkname[PATH_MAX]; 302 #if !HAVE_LINK 303 #undef HAVE_LINK 304 #define HAVE_LINK 1 305 #endif 306 #endif /* USE_SYMLINKS */ 307 308 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN); 309 char saved = '\0'; 310 311 static int call_count; 312 static time_t start_time; /* time at start of writes */ 313 314 #endif /* USE_HASHED_DB */ 315 316 char name_list[MAX_TERMINFO_LENGTH]; 317 char *first_name, *other_names; 318 char *ptr; 319 char *term_names = tp->term_names; 320 size_t name_size = strlen(term_names); 321 322 if (name_size == 0) { 323 _nc_syserr_abort("no terminal name found."); 324 } else if (name_size >= sizeof(name_list) - 1) { 325 _nc_syserr_abort("terminal name too long: %s", term_names); 326 } 327 328 _nc_STRCPY(name_list, term_names, sizeof(name_list)); 329 DEBUG(7, ("Name list = '%s'", name_list)); 330 331 first_name = name_list; 332 333 ptr = &name_list[name_size - 1]; 334 other_names = ptr + 1; 335 336 while (ptr > name_list && *ptr != '|') 337 ptr--; 338 339 if (ptr != name_list) { 340 *ptr = '\0'; 341 342 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) { 343 /* EMPTY */ ; 344 } 345 346 if (*ptr == '\0') 347 other_names = ptr; 348 else { 349 *ptr = '\0'; 350 other_names = ptr + 1; 351 } 352 } 353 354 DEBUG(7, ("First name = '%s'", first_name)); 355 DEBUG(7, ("Other names = '%s'", other_names)); 356 357 _nc_set_type(first_name); 358 359 #if USE_HASHED_DB 360 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) { 361 DB *capdb = _nc_db_open(_nc_tic_dir(NULL), TRUE); 362 DBT key, data; 363 364 if (capdb != NULL) { 365 buffer[0] = 0; 366 367 memset(&key, 0, sizeof(key)); 368 key.data = term_names; 369 key.size = name_size; 370 371 memset(&data, 0, sizeof(data)); 372 data.data = buffer; 373 data.size = offset + 1; 374 375 _nc_db_put(capdb, &key, &data); 376 377 buffer[0] = 2; 378 379 key.data = name_list; 380 key.size = strlen(name_list); 381 382 _nc_STRCPY(buffer + 1, 383 term_names, 384 sizeof(buffer) - 1); 385 data.size = name_size + 1; 386 387 total_size += (int) data.size; 388 total_parts++; 389 _nc_db_put(capdb, &key, &data); 390 391 while (*other_names != '\0') { 392 ptr = other_names++; 393 assert(ptr < buffer + sizeof(buffer) - 1); 394 while (*other_names != '|' && *other_names != '\0') 395 other_names++; 396 397 if (*other_names != '\0') 398 *(other_names++) = '\0'; 399 400 key.data = ptr; 401 key.size = strlen(ptr); 402 403 total_size += (int) data.size; 404 total_parts++; 405 _nc_db_put(capdb, &key, &data); 406 } 407 } 408 } 409 #else /* !USE_HASHED_DB */ 410 if (call_count++ == 0) { 411 start_time = 0; 412 } 413 414 if (strlen(first_name) >= limit2) { 415 _nc_warning("terminal name too long."); 416 saved = first_name[limit2]; 417 first_name[limit2] = '\0'; 418 } 419 420 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename)) 421 LEAF_FMT "/%.*s", UChar(first_name[0]), 422 (int) (sizeof(filename) - (LEAF_LEN + 2)), 423 first_name); 424 425 if (saved) 426 first_name[limit2] = saved; 427 428 /* 429 * Has this primary name been written since the first call to 430 * write_entry()? If so, the newer write will step on the older, 431 * so warn the user. 432 */ 433 if (start_time > 0 && 434 stat(filename, &statbuf) >= 0 435 && statbuf.st_mtime >= start_time) { 436 #if HAVE_LINK && !USE_SYMLINKS 437 /* 438 * If the file has more than one link, the reason for the previous 439 * write could be that the current primary name used to be an alias for 440 * the previous entry. In that case, unlink the file so that we will 441 * not modify the previous entry as we write this one. 442 */ 443 if (statbuf.st_nlink > 1) { 444 _nc_warning("name redefined."); 445 unlink(filename); 446 } else { 447 _nc_warning("name multiply defined."); 448 } 449 #else 450 _nc_warning("name multiply defined."); 451 #endif 452 } 453 454 check_writeable(first_name[0]); 455 write_file(filename, tp); 456 457 if (start_time == 0) { 458 if (stat(filename, &statbuf) == -1 459 || (start_time = statbuf.st_mtime) == 0) { 460 _nc_syserr_abort("error obtaining time from %s/%s", 461 _nc_tic_dir(NULL), filename); 462 } 463 } 464 while (*other_names != '\0') { 465 ptr = other_names++; 466 while (*other_names != '|' && *other_names != '\0') 467 other_names++; 468 469 if (*other_names != '\0') 470 *(other_names++) = '\0'; 471 472 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) { 473 _nc_warning("terminal alias %s too long.", ptr); 474 continue; 475 } 476 if (strchr(ptr, '/') != NULL) { 477 _nc_warning("cannot link alias %s.", ptr); 478 continue; 479 } 480 481 check_writeable(ptr[0]); 482 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname)) 483 LEAF_FMT "/%.*s", ptr[0], 484 (int) sizeof(linkname) - (2 + LEAF_LEN), ptr); 485 486 if (strcmp(filename, linkname) == 0) { 487 _nc_warning("self-synonym ignored"); 488 } 489 #if !LINK_TOUCHES 490 else if (stat(linkname, &statbuf) >= 0 && 491 statbuf.st_mtime < start_time) { 492 _nc_warning("alias %s multiply defined.", ptr); 493 } 494 #endif 495 else if (_nc_access(linkname, W_OK) == 0) 496 #if HAVE_LINK 497 { 498 int code; 499 #if USE_SYMLINKS 500 #define MY_SIZE sizeof(symlinkname) - 1 501 if (first_name[0] == linkname[0]) { 502 _nc_STRNCPY(symlinkname, first_name, MY_SIZE); 503 } else { 504 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname)); 505 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3); 506 } 507 symlinkname[MY_SIZE] = '\0'; 508 #endif /* USE_SYMLINKS */ 509 #if HAVE_REMOVE 510 code = remove(linkname); 511 #else 512 code = unlink(linkname); 513 #endif 514 if (code != 0 && errno == ENOENT) 515 code = 0; 516 #if USE_SYMLINKS 517 if (symlink(symlinkname, linkname) < 0) 518 #else 519 if (link(filename, linkname) < 0) 520 #endif /* USE_SYMLINKS */ 521 { 522 /* 523 * If there wasn't anything there, and we cannot 524 * link to the target because it is the same as the 525 * target, then the source must be on a filesystem 526 * that uses caseless filenames, such as Win32, etc. 527 */ 528 if (code == 0 && errno == EEXIST) 529 _nc_warning("can't link %s to %s", filename, linkname); 530 else if (code == 0 && (errno == EPERM || errno == ENOENT)) 531 write_file(linkname, tp); 532 else { 533 #if MIXEDCASE_FILENAMES 534 _nc_syserr_abort("cannot link %s to %s", filename, linkname); 535 #else 536 _nc_warning("cannot link %s to %s (errno=%d)", filename, 537 linkname, errno); 538 #endif 539 } 540 } else { 541 DEBUG(1, ("Linked %s", linkname)); 542 } 543 } 544 #else /* just make copies */ 545 write_file(linkname, tp); 546 #endif /* HAVE_LINK */ 547 } 548 #endif /* USE_HASHED_DB */ 549 } 550 551 static size_t 552 fake_write(char *dst, 553 unsigned *offset, 554 size_t limit, 555 char *src, 556 size_t want, 557 size_t size) 558 { 559 size_t have = (limit - *offset); 560 561 want *= size; 562 if (have > 0) { 563 if (want > have) 564 want = have; 565 memcpy(dst + *offset, src, want); 566 *offset += (unsigned) want; 567 } else { 568 want = 0; 569 } 570 return (want / size); 571 } 572 573 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size) 574 575 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */ 576 #define HI(x) ((x) / 256) 577 #define LO(x) ((x) % 256) 578 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \ 579 (p)[1] = (unsigned char)HI(x) 580 581 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1) 582 583 static int 584 compute_offsets(char **Strings, size_t strmax, short *offsets) 585 { 586 int nextfree = 0; 587 size_t i; 588 589 for (i = 0; i < strmax; i++) { 590 if (Strings[i] == ABSENT_STRING) { 591 offsets[i] = -1; 592 } else if (Strings[i] == CANCELLED_STRING) { 593 offsets[i] = -2; 594 } else { 595 offsets[i] = (short) nextfree; 596 nextfree += (int) strlen(Strings[i]) + 1; 597 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i, 598 _nc_visbuf(Strings[i]), (int) nextfree)); 599 } 600 } 601 return nextfree; 602 } 603 604 static size_t 605 convert_shorts(unsigned char *buf, short *Numbers, size_t count) 606 { 607 size_t i; 608 for (i = 0; i < count; i++) { 609 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */ 610 buf[2 * i] = buf[2 * i + 1] = 0377; 611 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */ 612 buf[2 * i] = 0376; 613 buf[2 * i + 1] = 0377; 614 } else { 615 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]); 616 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i])); 617 } 618 } 619 return SIZEOF_SHORT; 620 } 621 622 #if NCURSES_EXT_NUMBERS 623 static size_t 624 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count) 625 { 626 size_t i, j; 627 size_t size = SIZEOF_SHORT; 628 for (i = 0; i < count; i++) { 629 unsigned value = (unsigned) Numbers[i]; 630 TRACE_NUM(i); 631 for (j = 0; j < size; ++j) { 632 *buf++ = value & 0xff; 633 value >>= 8; 634 } 635 } 636 return size; 637 } 638 639 static size_t 640 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count) 641 { 642 size_t i, j; 643 size_t size = SIZEOF_INT2; 644 for (i = 0; i < count; i++) { 645 unsigned value = (unsigned) Numbers[i]; 646 TRACE_NUM(i); 647 for (j = 0; j < size; ++j) { 648 *buf++ = value & 0xff; 649 value >>= 8; 650 } 651 } 652 return size; 653 } 654 #endif 655 656 #define even_boundary(value) \ 657 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1) 658 659 #if NCURSES_XNAMES 660 static unsigned 661 extended_Booleans(TERMTYPE2 *tp) 662 { 663 unsigned result = 0; 664 unsigned i; 665 666 for (i = 0; i < tp->ext_Booleans; ++i) { 667 if (tp->Booleans[BOOLCOUNT + i] == TRUE) 668 result = (i + 1); 669 } 670 return result; 671 } 672 673 static unsigned 674 extended_Numbers(TERMTYPE2 *tp) 675 { 676 unsigned result = 0; 677 unsigned i; 678 679 for (i = 0; i < tp->ext_Numbers; ++i) { 680 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC) 681 result = (i + 1); 682 } 683 return result; 684 } 685 686 static unsigned 687 extended_Strings(TERMTYPE2 *tp) 688 { 689 unsigned short result = 0; 690 unsigned short i; 691 692 for (i = 0; i < tp->ext_Strings; ++i) { 693 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING) 694 result = (unsigned short) (i + 1); 695 } 696 return result; 697 } 698 699 /* 700 * _nc_align_termtype() will extend entries that are referenced in a use= 701 * clause - discard the unneeded data. 702 */ 703 static bool 704 extended_object(TERMTYPE2 *tp) 705 { 706 bool result = FALSE; 707 708 if (_nc_user_definable) { 709 result = ((extended_Booleans(tp) 710 + extended_Numbers(tp) 711 + extended_Strings(tp)) != 0); 712 } 713 return result; 714 } 715 #endif 716 717 NCURSES_EXPORT(int) 718 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit) 719 { 720 char *namelist; 721 size_t namelen, boolmax, nummax, strmax, numlen; 722 char zero = '\0'; 723 size_t i; 724 int nextfree; 725 short offsets[MAX_ENTRY_SIZE / 2]; 726 unsigned char buf[MAX_ENTRY_SIZE]; 727 unsigned last_bool = BOOLWRITE; 728 unsigned last_num = NUMWRITE; 729 unsigned last_str = STRWRITE; 730 #if NCURSES_EXT_NUMBERS 731 bool need_ints = FALSE; 732 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t); 733 #else 734 #define convert_numbers convert_shorts 735 #endif 736 737 #if NCURSES_XNAMES 738 /* 739 * Normally we limit the list of values to exclude the "obsolete" 740 * capabilities. However, if we are accepting extended names, add 741 * these as well, since they are used for supporting translation 742 * to/from termcap. 743 */ 744 if (_nc_user_definable) { 745 last_bool = BOOLCOUNT; 746 last_num = NUMCOUNT; 747 last_str = STRCOUNT; 748 } 749 #endif 750 751 namelist = tp->term_names; 752 namelen = strlen(namelist) + 1; 753 754 boolmax = 0; 755 for (i = 0; i < last_bool; i++) { 756 if (tp->Booleans[i] == TRUE) { 757 boolmax = i + 1; 758 } 759 } 760 761 nummax = 0; 762 for (i = 0; i < last_num; i++) { 763 if (tp->Numbers[i] != ABSENT_NUMERIC) { 764 nummax = i + 1; 765 #if NCURSES_EXT_NUMBERS 766 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) { 767 need_ints = TRUE; 768 } 769 #endif 770 } 771 } 772 773 strmax = 0; 774 for (i = 0; i < last_str; i++) { 775 if (tp->Strings[i] != ABSENT_STRING) 776 strmax = i + 1; 777 } 778 779 nextfree = compute_offsets(tp->Strings, strmax, offsets); 780 781 /* fill in the header */ 782 #if NCURSES_EXT_NUMBERS 783 if (need_ints) { 784 convert_numbers = convert_32bit; 785 LITTLE_ENDIAN(buf, MAGIC2); 786 } else { 787 convert_numbers = convert_16bit; 788 LITTLE_ENDIAN(buf, MAGIC); 789 } 790 #else 791 LITTLE_ENDIAN(buf, MAGIC); 792 #endif 793 namelen = Min(namelen, MAX_NAME_SIZE + 1); 794 LITTLE_ENDIAN(buf + 2, namelen); 795 LITTLE_ENDIAN(buf + 4, boolmax); 796 LITTLE_ENDIAN(buf + 6, nummax); 797 LITTLE_ENDIAN(buf + 8, strmax); 798 LITTLE_ENDIAN(buf + 10, nextfree); 799 800 /* write out the header */ 801 TRACE_OUT(("Header of %s @%d", namelist, *offset)); 802 if (Write(buf, 12, 1) != 1 803 || Write(namelist, sizeof(char), namelen) != namelen) { 804 return (ERR); 805 } 806 807 for (i = 0; i < boolmax; i++) { 808 if (tp->Booleans[i] == TRUE) { 809 buf[i] = TRUE; 810 } else { 811 buf[i] = FALSE; 812 } 813 } 814 if (Write(buf, sizeof(char), boolmax) != boolmax) { 815 return (ERR); 816 } 817 818 if (even_boundary(namelen + boolmax)) { 819 return (ERR); 820 } 821 822 TRACE_OUT(("Numerics begin at %04x", *offset)); 823 824 /* the numerics */ 825 numlen = convert_numbers(buf, tp->Numbers, nummax); 826 if (Write(buf, numlen, nummax) != nummax) { 827 return (ERR); 828 } 829 830 TRACE_OUT(("String offsets begin at %04x", *offset)); 831 832 /* the string offsets */ 833 convert_shorts(buf, offsets, strmax); 834 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) { 835 return (ERR); 836 } 837 838 TRACE_OUT(("String table begins at %04x", *offset)); 839 840 /* the strings */ 841 for (i = 0; i < strmax; i++) { 842 if (VALID_STRING(tp->Strings[i])) { 843 if (!WRITE_STRING(tp->Strings[i])) { 844 return (ERR); 845 } 846 } 847 } 848 849 #if NCURSES_XNAMES 850 if (extended_object(tp)) { 851 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp); 852 unsigned ext_usage = ext_total; 853 854 if (even_boundary(nextfree)) { 855 return (ERR); 856 } 857 858 nextfree = compute_offsets(tp->Strings + STRCOUNT, 859 (size_t) tp->ext_Strings, 860 offsets); 861 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree)); 862 863 if (tp->ext_Strings >= SIZEOF(offsets)) { 864 return (ERR); 865 } 866 867 nextfree += compute_offsets(tp->ext_Names, 868 (size_t) ext_total, 869 offsets + tp->ext_Strings); 870 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree)); 871 strmax = tp->ext_Strings + ext_total; 872 for (i = 0; i < tp->ext_Strings; ++i) { 873 if (VALID_STRING(tp->Strings[i + STRCOUNT])) { 874 ext_usage++; 875 } 876 } 877 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax)); 878 879 /* 880 * Write the extended header 881 */ 882 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans); 883 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers); 884 LITTLE_ENDIAN(buf + 4, tp->ext_Strings); 885 LITTLE_ENDIAN(buf + 6, ext_usage); 886 LITTLE_ENDIAN(buf + 8, nextfree); 887 TRACE_OUT(("WRITE extended-header @%d", *offset)); 888 if (Write(buf, 10, 1) != 1) { 889 return (ERR); 890 } 891 892 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset)); 893 if (tp->ext_Booleans 894 && Write(tp->Booleans + BOOLCOUNT, sizeof(char), 895 tp->ext_Booleans) != tp->ext_Booleans) { 896 return (ERR); 897 } 898 899 if (even_boundary(tp->ext_Booleans)) { 900 return (ERR); 901 } 902 903 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset)); 904 if (tp->ext_Numbers) { 905 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers); 906 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) { 907 return (ERR); 908 } 909 } 910 911 /* 912 * Convert the offsets for the ext_Strings and ext_Names tables, 913 * in that order. 914 */ 915 convert_shorts(buf, offsets, strmax); 916 TRACE_OUT(("WRITE offsets @%d", *offset)); 917 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) { 918 return (ERR); 919 } 920 921 /* 922 * Write the string table after the offset tables so we do not 923 * have to do anything about alignment. 924 */ 925 for (i = 0; i < tp->ext_Strings; i++) { 926 if (VALID_STRING(tp->Strings[i + STRCOUNT])) { 927 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i, 928 _nc_visbuf(tp->Strings[i + STRCOUNT]))); 929 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) { 930 return (ERR); 931 } 932 } 933 } 934 935 /* 936 * Write the extended names 937 */ 938 for (i = 0; i < ext_total; i++) { 939 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i])); 940 if (!WRITE_STRING(tp->ext_Names[i])) { 941 return (ERR); 942 } 943 } 944 945 } 946 #endif /* NCURSES_XNAMES */ 947 948 total_written++; 949 total_parts++; 950 total_size = total_size + (int) (*offset + 1); 951 return (OK); 952 } 953 954 /* 955 * Returns the total number of entries written by this process 956 */ 957 NCURSES_EXPORT(int) 958 _nc_tic_written(void) 959 { 960 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size", 961 total_written, total_parts, total_size)); 962 return total_written; 963 } 964