1 /* $OpenBSD: write_entry.c,v 1.12 2003/03/18 16:55:54 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * write_entry.c -- write a terminfo structure onto the file system 38 */ 39 40 #include <curses.priv.h> 41 42 #include <sys/stat.h> 43 44 #include <tic.h> 45 #include <term_entry.h> 46 47 #ifndef S_ISDIR 48 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR) 49 #endif 50 51 #if 0 52 #define TRACE_OUT(p) DEBUG(2, p) 53 #else 54 #define TRACE_OUT(p) /*nothing */ 55 #endif 56 57 MODULE_ID("$From: write_entry.c,v 1.56 2000/12/10 02:55:08 tom Exp $") 58 59 static int total_written; 60 61 static int write_object(FILE *, TERMTYPE *); 62 63 static void 64 write_file(char *filename, TERMTYPE * tp) 65 { 66 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0; 67 if (fp == 0) { 68 perror(filename); 69 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename); 70 } 71 DEBUG(1, ("Created %s", filename)); 72 73 if (write_object(fp, tp) == ERR) { 74 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename); 75 } 76 fclose(fp); 77 } 78 79 /* 80 * make_directory(char *path) 81 * 82 * Make a directory if it doesn't exist. 83 */ 84 static int 85 make_directory(const char *path) 86 { 87 int rc; 88 struct stat statbuf; 89 char fullpath[PATH_MAX]; 90 const char *destination = _nc_tic_dir(0); 91 92 if (path == destination || *path == '/') { 93 if (strlcpy(fullpath, path, sizeof(fullpath)) >= sizeof(fullpath)) 94 return (-1); 95 } else { 96 if (strlen(destination) + strlen(path) + 2 > sizeof(fullpath)) 97 return (-1); 98 (void) snprintf(fullpath, sizeof(fullpath), "%s/%s", destination, path); 99 } 100 101 if ((rc = stat(path, &statbuf)) < 0) { 102 rc = mkdir(path, 0777); 103 } else { 104 if (_nc_access(path, R_OK | W_OK | X_OK) < 0) { 105 rc = -1; /* permission denied */ 106 } else if (!(S_ISDIR(statbuf.st_mode))) { 107 rc = -1; /* not a directory */ 108 } 109 } 110 return rc; 111 } 112 113 NCURSES_EXPORT(void) 114 _nc_set_writedir(char *dir) 115 /* set the write directory for compiled entries */ 116 { 117 const char *destination; 118 char actual[PATH_MAX]; 119 120 if (dir == 0 121 && use_terminfo_vars()) 122 dir = getenv("TERMINFO"); 123 124 if (dir != 0) 125 (void) _nc_tic_dir(dir); 126 127 destination = _nc_tic_dir(0); 128 if (make_directory(destination) < 0) { 129 char *home = _nc_home_terminfo(); 130 131 if (home != 0) { 132 destination = home; 133 if (make_directory(destination) < 0) 134 _nc_err_abort("%s: permission denied (errno %d)", 135 destination, errno); 136 } 137 } 138 139 /* 140 * Note: because of this code, this logic should be exercised 141 * *once only* per run. 142 */ 143 if (chdir(_nc_tic_dir(destination)) < 0 144 || getcwd(actual, sizeof(actual)) == 0) 145 _nc_err_abort("%s: not a directory", destination); 146 _nc_keep_tic_dir(strdup(actual)); 147 } 148 149 /* 150 * check_writeable(char code) 151 * 152 * Miscellaneous initialisations 153 * 154 * Check for access rights to destination directories 155 * Create any directories which don't exist. 156 * Note: there's no reason to return the result of make_directory(), since 157 * this function is called only in instances where that has to succeed. 158 * 159 */ 160 161 static void 162 check_writeable(int code) 163 { 164 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 165 static bool verified[sizeof(dirnames)]; 166 167 char dir[2]; 168 char *s = 0; 169 170 if (code == 0 || (s = strchr(dirnames, code)) == 0) 171 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code); 172 173 if (verified[s - dirnames]) 174 return; 175 176 dir[0] = code; 177 dir[1] = '\0'; 178 if (make_directory(dir) < 0) { 179 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir); 180 } 181 182 verified[s - dirnames] = TRUE; 183 } 184 185 /* 186 * _nc_write_entry() 187 * 188 * Save the compiled version of a description in the filesystem. 189 * 190 * make a copy of the name-list 191 * break it up into first-name and all-but-last-name 192 * creat(first-name) 193 * write object information to first-name 194 * close(first-name) 195 * for each name in all-but-last-name 196 * link to first-name 197 * 198 * Using 'time()' to obtain a reference for file timestamps is unreliable, 199 * e.g., with NFS, because the filesystem may have a different time 200 * reference. We check for pre-existence of links by latching the first 201 * timestamp from a file that we create. 202 * 203 * The _nc_warning() calls will report a correct line number only if 204 * _nc_curr_line is properly set before the write_entry() call. 205 */ 206 207 void 208 _nc_write_entry(TERMTYPE * const tp) 209 { 210 struct stat statbuf; 211 char name_list[MAX_TERMINFO_LENGTH]; 212 char *first_name, *other_names; 213 char *ptr; 214 char filename[PATH_MAX]; 215 char linkname[PATH_MAX]; 216 #if USE_SYMLINKS 217 char symlinkname[PATH_MAX]; 218 #endif /* USE_SYMLINKS */ 219 static int call_count; 220 static time_t start_time; /* time at start of writes */ 221 222 if (call_count++ == 0) { 223 start_time = 0; 224 } 225 226 (void) strlcpy(name_list, tp->term_names, sizeof(name_list)); 227 DEBUG(7, ("Name list = '%s'", name_list)); 228 229 first_name = name_list; 230 231 ptr = &name_list[strlen(name_list) - 1]; 232 other_names = ptr + 1; 233 234 while (ptr > name_list && *ptr != '|') 235 ptr--; 236 237 if (ptr != name_list) { 238 *ptr = '\0'; 239 240 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) 241 continue; 242 243 if (*ptr == '\0') 244 other_names = ptr; 245 else { 246 *ptr = '\0'; 247 other_names = ptr + 1; 248 } 249 } 250 251 DEBUG(7, ("First name = '%s'", first_name)); 252 DEBUG(7, ("Other names = '%s'", other_names)); 253 254 _nc_set_type(first_name); 255 256 if (strlen(first_name) > sizeof(filename) - 3) 257 _nc_warning("terminal name too long."); 258 259 snprintf(filename, sizeof(filename), "%c/%s", first_name[0], first_name); 260 261 /* 262 * Has this primary name been written since the first call to 263 * write_entry()? If so, the newer write will step on the older, 264 * so warn the user. 265 */ 266 if (start_time > 0 && 267 stat(filename, &statbuf) >= 0 268 && statbuf.st_mtime >= start_time) { 269 _nc_warning("name multiply defined."); 270 } 271 272 check_writeable(first_name[0]); 273 write_file(filename, tp); 274 275 if (start_time == 0) { 276 if (stat(filename, &statbuf) < 0 277 || (start_time = statbuf.st_mtime) == 0) { 278 _nc_syserr_abort("error obtaining time from %s/%s", 279 _nc_tic_dir(0), filename); 280 } 281 } 282 while (*other_names != '\0') { 283 ptr = other_names++; 284 while (*other_names != '|' && *other_names != '\0') 285 other_names++; 286 287 if (*other_names != '\0') 288 *(other_names++) = '\0'; 289 290 if (strlen(ptr) > sizeof(linkname) - 3) { 291 _nc_warning("terminal alias %s too long.", ptr); 292 continue; 293 } 294 if (strchr(ptr, '/') != 0) { 295 _nc_warning("cannot link alias %s.", ptr); 296 continue; 297 } 298 299 check_writeable(ptr[0]); 300 snprintf(linkname, sizeof(linkname), "%c/%s", ptr[0], ptr); 301 302 if (strcmp(filename, linkname) == 0) { 303 _nc_warning("self-synonym ignored"); 304 } else if (stat(linkname, &statbuf) >= 0 && 305 statbuf.st_mtime < start_time) { 306 _nc_warning("alias %s multiply defined.", ptr); 307 } else if (_nc_access(linkname, W_OK) == 0) 308 #if HAVE_LINK 309 { 310 int code; 311 #if USE_SYMLINKS 312 strlcpy(symlinkname, "../", sizeof(symlinkname)); 313 strlcat(symlinkname, filename, sizeof(symlinkname)); 314 #endif /* USE_SYMLINKS */ 315 #if HAVE_REMOVE 316 code = remove(linkname); 317 #else 318 code = unlink(linkname); 319 #endif 320 if (code != 0 && errno == ENOENT) 321 code = 0; 322 #if USE_SYMLINKS 323 if (symlink(symlinkname, linkname) < 0) 324 #else 325 if (link(filename, linkname) < 0) 326 #endif /* USE_SYMLINKS */ 327 { 328 /* 329 * If there wasn't anything there, and we cannot 330 * link to the target because it is the same as the 331 * target, then the source must be on a filesystem 332 * that uses caseless filenames, such as Win32, etc. 333 */ 334 if (code == 0 && errno == EEXIST) 335 _nc_warning("can't link %s to %s", filename, linkname); 336 else if (code == 0 && (errno == EPERM || errno == ENOENT)) 337 write_file(linkname, tp); 338 else { 339 #if MIXEDCASE_FILENAMES 340 _nc_syserr_abort("can't link %s to %s", filename, linkname); 341 #else 342 _nc_warning("can't link %s to %s (errno=%d)", filename, 343 linkname, errno); 344 #endif 345 } 346 } else { 347 DEBUG(1, ("Linked %s", linkname)); 348 } 349 } 350 #else /* just make copies */ 351 write_file(linkname, tp); 352 #endif /* HAVE_LINK */ 353 } 354 } 355 356 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */ 357 #define HI(x) ((x) / 256) 358 #define LO(x) ((x) % 256) 359 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x) 360 361 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1) 362 363 static int 364 compute_offsets(char **Strings, int strmax, short *offsets) 365 { 366 size_t nextfree = 0; 367 int i; 368 369 for (i = 0; i < strmax; i++) { 370 if (Strings[i] == ABSENT_STRING) { 371 offsets[i] = -1; 372 } else if (Strings[i] == CANCELLED_STRING) { 373 offsets[i] = -2; 374 } else { 375 offsets[i] = nextfree; 376 nextfree += strlen(Strings[i]) + 1; 377 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree)); 378 } 379 } 380 return nextfree; 381 } 382 383 static void 384 convert_shorts(unsigned char *buf, short *Numbers, int count) 385 { 386 int i; 387 for (i = 0; i < count; i++) { 388 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */ 389 buf[2 * i] = buf[2 * i + 1] = 0377; 390 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */ 391 buf[2 * i] = 0376; 392 buf[2 * i + 1] = 0377; 393 } else { 394 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]); 395 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i])); 396 } 397 } 398 } 399 400 #define even_boundary(value) \ 401 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1) 402 403 static int 404 write_object(FILE * fp, TERMTYPE * tp) 405 { 406 char *namelist; 407 size_t namelen, boolmax, nummax, strmax; 408 char zero = '\0'; 409 size_t i; 410 short nextfree; 411 short offsets[MAX_ENTRY_SIZE / 2]; 412 unsigned char buf[MAX_ENTRY_SIZE]; 413 unsigned last_bool = BOOLWRITE; 414 unsigned last_num = NUMWRITE; 415 unsigned last_str = STRWRITE; 416 417 #if NCURSES_XNAMES 418 /* 419 * Normally we limit the list of values to exclude the "obsolete" 420 * capabilities. However, if we are accepting extended names, add 421 * these as well, since they are used for supporting translation 422 * to/from termcap. 423 */ 424 if (_nc_user_definable) { 425 last_bool = BOOLCOUNT; 426 last_num = NUMCOUNT; 427 last_str = STRCOUNT; 428 } 429 #endif 430 431 namelist = tp->term_names; 432 namelen = strlen(namelist) + 1; 433 434 boolmax = 0; 435 for (i = 0; i < last_bool; i++) { 436 if (tp->Booleans[i] == TRUE) 437 boolmax = i + 1; 438 } 439 440 nummax = 0; 441 for (i = 0; i < last_num; i++) { 442 if (tp->Numbers[i] != ABSENT_NUMERIC) 443 nummax = i + 1; 444 } 445 446 strmax = 0; 447 for (i = 0; i < last_str; i++) { 448 if (tp->Strings[i] != ABSENT_STRING) 449 strmax = i + 1; 450 } 451 452 nextfree = compute_offsets(tp->Strings, strmax, offsets); 453 454 /* fill in the header */ 455 LITTLE_ENDIAN(buf, MAGIC); 456 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1)); 457 LITTLE_ENDIAN(buf + 4, boolmax); 458 LITTLE_ENDIAN(buf + 6, nummax); 459 LITTLE_ENDIAN(buf + 8, strmax); 460 LITTLE_ENDIAN(buf + 10, nextfree); 461 462 /* write out the header */ 463 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp))); 464 if (fwrite(buf, 12, 1, fp) != 1 465 || fwrite(namelist, sizeof(char), namelen, fp) != namelen) 466 return (ERR); 467 468 for (i = 0; i < boolmax; i++) 469 if (tp->Booleans[i] == TRUE) 470 buf[i] = TRUE; 471 else 472 buf[i] = FALSE; 473 if (fwrite(buf, sizeof(char), boolmax, fp) != boolmax) 474 return (ERR); 475 476 if (even_boundary(namelen + boolmax)) 477 return (ERR); 478 479 TRACE_OUT(("Numerics begin at %04lx", ftell(fp))); 480 481 /* the numerics */ 482 convert_shorts(buf, tp->Numbers, nummax); 483 if (fwrite(buf, 2, nummax, fp) != nummax) 484 return (ERR); 485 486 TRACE_OUT(("String offsets begin at %04lx", ftell(fp))); 487 488 /* the string offsets */ 489 convert_shorts(buf, offsets, strmax); 490 if (fwrite(buf, 2, strmax, fp) != strmax) 491 return (ERR); 492 493 TRACE_OUT(("String table begins at %04lx", ftell(fp))); 494 495 /* the strings */ 496 for (i = 0; i < strmax; i++) 497 if (VALID_STRING(tp->Strings[i])) 498 if (!WRITE_STRING(tp->Strings[i])) 499 return (ERR); 500 501 #if NCURSES_XNAMES 502 if (NUM_EXT_NAMES(tp)) { 503 unsigned extcnt = NUM_EXT_NAMES(tp); 504 505 if (even_boundary(nextfree)) 506 return (ERR); 507 508 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets); 509 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree)); 510 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings); 511 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree)); 512 strmax = tp->ext_Strings + extcnt; 513 514 /* 515 * Write the extended header 516 */ 517 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans); 518 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers); 519 LITTLE_ENDIAN(buf + 4, tp->ext_Strings); 520 LITTLE_ENDIAN(buf + 6, strmax); 521 LITTLE_ENDIAN(buf + 8, nextfree); 522 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp))); 523 if (fwrite(buf, 10, 1, fp) != 1) 524 return (ERR); 525 526 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp))); 527 if (tp->ext_Booleans 528 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char), 529 tp->ext_Booleans, fp) != tp->ext_Booleans) 530 return (ERR); 531 532 if (even_boundary(tp->ext_Booleans)) 533 return (ERR); 534 535 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp))); 536 if (tp->ext_Numbers) { 537 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers); 538 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers) 539 return (ERR); 540 } 541 542 /* 543 * Convert the offsets for the ext_Strings and ext_Names tables, 544 * in that order. 545 */ 546 convert_shorts(buf, offsets, strmax); 547 TRACE_OUT(("WRITE offsets @%ld", ftell(fp))); 548 if (fwrite(buf, 2, strmax, fp) != strmax) 549 return (ERR); 550 551 /* 552 * Write the string table after the offset tables so we do not 553 * have to do anything about alignment. 554 */ 555 for (i = 0; i < tp->ext_Strings; i++) { 556 if (VALID_STRING(tp->Strings[i + STRCOUNT])) { 557 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i, 558 _nc_visbuf(tp->Strings[i + STRCOUNT]))); 559 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) 560 return (ERR); 561 } 562 } 563 564 /* 565 * Write the extended names 566 */ 567 for (i = 0; i < extcnt; i++) { 568 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i])); 569 if (!WRITE_STRING(tp->ext_Names[i])) 570 return (ERR); 571 } 572 573 } 574 #endif /* NCURSES_XNAMES */ 575 576 total_written++; 577 return (OK); 578 } 579 580 /* 581 * Returns the total number of entries written by this process 582 */ 583 NCURSES_EXPORT(int) 584 _nc_tic_written(void) 585 { 586 return total_written; 587 } 588